📌  相关文章
📜  计算替换数组元素的不同方法,使数组的乘积变得偶数

📅  最后修改于: 2021-09-04 07:56:56             🧑  作者: Mango

给定一个由N 个奇数组成的数组arr[] ,任务是通过重复将任何一组元素更改为任何值来计算使所有数组元素的乘积为偶数的不同方法的数量。由于计数可能非常大,请将其打印为模10 9 + 7

例子:

方法:解决给定问题的想法是基于这样的观察:只有当数组中至少存在一个偶数元素时,数组的乘积才是偶数。因此,不同方式的总数可以通过给定数组的不同子集的数量来计算。

下面是上述方法的实现:

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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live