给定两个正整数L和R ,任务是找到在[L,R]范围内的有序对的计数,以使每对元素的总和为偶数。
例子:
Input: L = 1, R =3
Output: 5
Explanation:
Pairs whose sum of elements are even and lies in the range [1, 3] are { (1, 3), (1, 1), (2, 2), (3, 3), (3, 1) }
Input: L = 1, R = 1
Output: 1
方法:可以使用以下事实来解决该问题:两个数字之和仅在两个数字均为偶数或两个数字均为奇数时才是偶数。请按照以下步骤解决此问题:
- 计算[L,R]范围内的所有偶数,例如cnt_even 。请按照以下步骤进行计算:
- 如果L为偶数,则cnt_even =(R / 2)–(L / 2)+1 。
- 如果L为奇数,则cnt_even =(R / 2)–(L / 2) 。
- 元素之和为偶数且对中的两个元素均为偶数的对的计数为count_even * count_even 。
- 类似地,计算范围为[L,R]的所有奇数,例如count_odd 。请按照以下步骤进行计算:
- 如果L是偶数,则count_odd =((R + 1)/ 2)–((L + 1)/ 2) 。
- 如果L为奇数,则count_odd =((R + 1)/ 2)–((L + 1)/ 2)+1 。
- 元素之和为偶数且对中的两个元素均为奇数的对的计数为count_odd * count_odd
- 最后,打印(count_odd * count_odd + count_even * count_even)的值
下面是上述方法的实现:
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to find the count of
// ordered pairs having even sum
int countPairs(int L, int R)
{
// Stores count of even numbers
// in the range [L, R]
int count_even;
// If L is even
if (L % 2 == 0) {
// Update count_even
count_even = (R / 2) - (L / 2) + 1;
}
else {
// Update count_odd
count_even = (R / 2) - (L / 2);
}
// Stores count of odd numbers
// in the range [L, R]
int count_odd;
if (L % 2 == 0) {
// Update count_odd
count_odd = ((R + 1) / 2) - ((L + 1) / 2);
}
else {
// Update count_odd
count_odd = ((R + 1) / 2) - ((L + 1) / 2) + 1;
}
// Stores count of pairs whose sum is even and
// both elements of the pairs are also even
count_even *= count_even;
// Stores count of pairs whose sum is even and
// both elements of the pairs are odd
count_odd *= count_odd;
// Print total ordered pairs whose sum is even
cout << count_even + count_odd;
}
// Driver Code
int main()
{
// Given L & R
int L = 1, R = 3;
// Function Call
countPairs(L, R);
return 0;
}
Java
// Java program for the above approach
class GFG
{
// Function to find the count of
// ordered pairs having even sum
static void countPairs(int L, int R)
{
// Stores count of even numbers
// in the range [L, R]
int count_even;
// If L is even
if (L % 2 == 0)
{
// Update count_even
count_even = (R / 2) - (L / 2) + 1;
}
else {
// Update count_odd
count_even = (R / 2) - (L / 2);
}
// Stores count of odd numbers
// in the range [L, R]
int count_odd;
if (L % 2 == 0)
{
// Update count_odd
count_odd = ((R + 1) / 2) - ((L + 1) / 2);
}
else
{
// Update count_odd
count_odd = ((R + 1) / 2) - ((L + 1) / 2) + 1;
}
// Stores count of pairs whose sum is even and
// both elements of the pairs are also even
count_even *= count_even;
// Stores count of pairs whose sum is even and
// both elements of the pairs are odd
count_odd *= count_odd;
// Print total ordered pairs whose sum is even
System.out.println(count_even + count_odd);
}
// Driver Code
public static void main (String[] args)
{
// Given L & R
int L = 1, R = 3;
// Function Call
countPairs(L, R);
}
}
// This code is contributed by AnkThon
Python3
# Python3 program for the above approach
# Function to find the count of
# ordered pairs having even sum
def countPairs(L, R):
# Stores count of even numbers
# in the range [L, R]
count_even = 0
# If L is even
if (L % 2 == 0):
# Update count_even
count_even = ((R // 2) -
(L // 2) + 1)
else:
# Update count_odd
count_even = ((R // 2) -
(L // 2))
# Stores count of odd numbers
# in the range [L, R]
count_odd = 0
if (L % 2 == 0):
# Update count_odd
count_odd = (((R + 1) // 2) -
((L + 1) // 2))
else:
# Update count_odd
count_odd = (((R + 1) // 2) -
((L + 1) // 2) + 1)
# Stores count of pairs whose sum is
# even and both elements of the pairs
# are also even
count_even *= count_even
# Stores count of pairs whose sum is
# even and both elements of the pairs
# are odd
count_odd *= count_odd
# Print total ordered pairs whose
# sum is even
print (count_even + count_odd)
# Driver Code
if __name__ == '__main__':
# Given L & R
L, R = 1, 3
# Function Call
countPairs(L, R)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the count of
// ordered pairs having even sum
static void countPairs(int L, int R)
{
// Stores count of even numbers
// in the range [L, R]
int count_even;
// If L is even
if (L % 2 == 0)
{
// Update count_even
count_even = (R / 2) - (L / 2) + 1;
}
else
{
// Update count_odd
count_even = (R / 2) - (L / 2);
}
// Stores count of odd numbers
// in the range [L, R]
int count_odd;
if (L % 2 == 0)
{
// Update count_odd
count_odd = ((R + 1) / 2) - ((L + 1) / 2);
}
else
{
// Update count_odd
count_odd = ((R + 1) / 2) - ((L + 1) / 2) + 1;
}
// Stores count of pairs whose sum is even and
// both elements of the pairs are also even
count_even *= count_even;
// Stores count of pairs whose sum is even and
// both elements of the pairs are odd
count_odd *= count_odd;
// Print total ordered pairs whose sum is even
Console.WriteLine(count_even + count_odd);
}
// Driver Code
public static void Main(String[] args)
{
// Given L & R
int L = 1, R = 3;
// Function Call
countPairs(L, R);
}
}
// This code is contributed by 29AjayKumar
输出:
5
时间复杂度: O(1)
辅助空间: O(1)