给定一个由N个奇数整数组成的数组arr [] ,任务是通过反复将元素的任何集合更改为任何值,来计算使所有数组元素的乘积均匀的不同方法。由于计数可能非常大,因此将其打印为10 9 + 7模。
例子:
Input: arr[] = {1, 3}
Output: 3
Explanation: All possible ways to make the product of array elements odd are as follows:
Replace arr[0] by any even integer. The array arr[] modifies to {even, 3}. Therefore, the product of the array = even * 3 = even.
Replace arr[1] by any even integer. The array arr[] modifies to {1, even}. Therefore, the product of the array = 1 * even = even.
Replace arr[0] and arr[1] by even integers. Since both array elements become even, the product of the array becomes even. Therefore, the total number of distinct ways to make the array even is 3.
Input: arr[] = {1, 2, 3, 4, 5}
Output: 31
方法:解决给定问题的想法是基于以下观察结果:只有当数组中至少存在一个偶数元素时,数组的乘积才是偶数。因此,可以通过给定数组的不同子集的数量来计算不同方式的总数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
#define M 1000000007
using namespace std;
// Function to find the value of (x^y)
long long power(long long x, long long y,
long long p)
{
// Stores the result
long long res = 1;
while (y > 0) {
// If y is odd, then
// multiply x with res
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1;
// Update x
x = (x * x) % p;
}
return res;
}
// Function to count the number of ways
// to make the product of an array even
// by replacing array elements
int totalOperations(int arr[], int N)
{
// Find the value ( 2 ^ N ) % M
long long res = power(2, N, M);
// Exclude empty subset
res--;
// Print the answer
cout << res;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
totalOperations(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
static long M = 1000000007;
// Function to find the value of (x^y)
static long power(long x, long y, long p)
{
// Stores the result
long res = 1;
while (y > 0)
{
// If y is odd, then
// multiply x with res
if ((y & 1) > 0)
res = (res * x) % p;
// y must be even now
y = y >> 1;
// Update x
x = (x * x) % p;
}
return res;
}
// Function to count the number of ways
// to make the product of an array even
// by replacing array elements
static int totalOperations(int arr[], int N)
{
// Find the value ( 2 ^ N ) % M
long res = power(2, N, M);
// Exclude empty subset
res--;
// Print the answer
System.out.print(res);
return 0;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = arr.length;
totalOperations(arr, N);
}
}
// This code is contributed by rag2127
Python3
# Python3 program for the above approach
M = 1000000007
# Function to find the value of (x^y)
def power(x, y, p):
global M
# Stores the result
res = 1
while (y > 0):
# If y is odd, then
# multiply x with res
if (y & 1):
res = (res * x) % p;
# y must be even now
y = y >> 1
# Update x
x = (x * x) % p
return res
# Function to count the number of ways
# to make the product of an array even
# by replacing array elements
def totalOperations(arr, N):
# Find the value ( 2 ^ N ) % M
res = power(2, N, M)
# Exclude empty subset
res-=1
# Print the answer
print (res)
# Driver Code
if __name__ == '__main__':
arr = [1, 2, 3, 4, 5]
N = len(arr)
totalOperations(arr, N)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG {
static long M = 1000000007;
// Function to find the value of (x^y)
static long power(long x, long y, long p)
{
// Stores the result
long res = 1;
while (y > 0)
{
// If y is odd, then
// multiply x with res
if ((y & 1) > 0)
res = (res * x) % p;
// y must be even now
y = y >> 1;
// Update x
x = (x * x) % p;
}
return res;
}
// Function to count the number of ways
// to make the product of an array even
// by replacing array elements
static int totalOperations(int[] arr, int N)
{
// Find the value ( 2 ^ N ) % M
long res = power(2, N, M);
// Exclude empty subset
res--;
// Print the answer
Console.Write(res);
return 0;
}
// Calculating gcd
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Driver code
static void Main()
{
int[] arr = { 1, 2, 3, 4, 5 };
int N = arr.Length;
totalOperations(arr, N);
}
}
// This code is contributed by sanjoy_62.
Javascript
31
时间复杂度: O(log N)
辅助空间: O(1)