给定N个正整数A 1 ,A 2 ,…,A N ,任务是确定表达式S的奇偶性。
对于给定的N个数字,表达式S给出为:
例子:
Input: N = 3, A1 = 2, A2 = 3, A3 = 1
Output: Even
Explanation:
S = 1 + (2 + 3 + 1) + (2*3 + 3*1 + 1*2) + (2*3*1) = 24, which is even
Input: N = 2, A1 = 2, A2 = 4
Output: Odd
Explanation:
S = 1 + (2 + 4) + (2 * 4) = 15, which is odd
幼稚的方法:针对此问题的幼稚的方法是在给定表达式中插入A i的所有值,并找到给定表达式的奇偶校验。此方法不适用于较高的N值,因为对于高阶数字而言,乘法不是一个常数运算。而且,该值可能会变得太大以至于可能导致整数溢出。
高效的方法:想法是对表达式执行一些处理,并将表达式简化为更简单的术语,以便可以在不计算值的情况下检查奇偶校验。令N =3。然后:
- 表达式S为:
1 + (A1 + A2 + A3) + ((A1 * A2) + (A2 * A3) + (A3 * A1) + (A1 * A2 * A3)
- 现在,将相同的表达式重构为如下形式:
(1 + A1) + (A2 + A1 * A2) + (A3 + A3 * A1) + (A2 * A3 + A1 * A2 * A3)
=> (1 + A1) + A2 * (1 + A1) + A3 * (1 + A1) + A2 * A3 * (1 + A1)
- 从上述等式中取(1 + A 1 )共通,
(1 + A1) * (1 + A2 + A2 + (A2 * A3))
=> (1 + A1) * (1 + A2 + A3 * (1 + A2)
- 最后,在采用(1 + A 2 )通用的情况下,最终表达式变为:
(1 + A1) * (1 + A2) * (1 + A3)
- 通过对称,对于N个元素,表达式S变为:
(1 + A1) * (1 + A2) * (1 + A3) … * (1 + AN)
- 显然,要使数字成为偶数,答案必须是偶数。众所周知,答案是偶数。
- 因此,其思想是检查给定输入中的任何数字是否为奇数。如果是,则在加一时,它变为偶数,并且值是偶数奇偶校验。
下面是上述方法的实现:
C++
// C++ program to determine the
// parity of the given mathematical
// expression
#include
using namespace std;
void getParity(
int n,
const vector& A)
{
// Iterating through the
// given integers
for (auto x : A) {
if (x & 1) {
// If any odd number
// is present, then S
// is even parity
cout << "Even" << endl;
return;
}
}
// Else, S is odd parity
cout << "Odd" << endl;
}
// Driver code
int main()
{
int N = 3;
vector A = { 2, 3, 1 };
getParity(N, A);
return 0;
}
Java
// Java program to determine the
// parity of the given mathematical
// expression
class GFG{
static void getParity(int n, int []A)
{
// Iterating through the
// given integers
for (int x : A)
{
if ((x & 1) == 1)
{
// If any odd number
// is present, then S
// is even parity
System.out.println("Even");
return;
}
}
// Else, S is odd parity
System.out.println("Odd");
}
// Driver code
public static void main(String[] args)
{
int N = 3;
int [] A = { 2, 3, 1 };
getParity(N, A);
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 program to determine the
# parity of the given mathematical
# expression
def getParity(n, A):
# Iterating through
# the given integers
for x in A:
if (x & 1):
# If any odd number
# is present, then S
# is even parity
print("Even")
return
# Else, S is odd parity
print("Odd")
# Driver code
if __name__ == '__main__':
N = 3
A = [ 2, 3, 1 ]
getParity(N, A)
# This code is contributed by mohit kumar 29
C#
// C# program to determine the
// parity of the given mathematical
// expression
using System;
public class GFG{
static void getParity(int n, int []A)
{
// Iterating through the
// given integers
foreach (int x in A)
{
if ((x & 1) == 1)
{
// If any odd number
// is present, then S
// is even parity
Console.WriteLine("Even");
return;
}
}
// Else, S is odd parity
Console.WriteLine("Odd");
}
// Driver code
public static void Main(string[] args)
{
int N = 3;
int [] A = { 2, 3, 1 };
getParity(N, A);
}
}
// This code is contributed by AnkitRai01
Even
时间复杂度: O(N) ,其中N是给定数字的数量。