📌  相关文章
📜  乘积为偶数的K长度子序列的计数

📅  最后修改于: 2021-05-17 04:54:32             🧑  作者: Mango

给定一个数组arr []和一个整数K ,任务是从大小为N的给定数组arr中找到长度为K的非空子序列的数量,以使子序列的乘积为偶数。
例子:

方法:
为了解决上述问题,我们必须找到长度为K的子序列的总数,并减去乘积为奇数的K个长度子序列的计数。

  1. 为了使子序列的乘积为奇数,我们必须选择K个数字为奇数。
  2. 因此,长度为K且乘积为奇数的子序列数可能是找到k个奇数,即“ o选择k ”或_{k}^{o}\textrm{C}
    其中o是子序列中的奇数计数。
  3. \text{So count of a subsequence with even product = } _{k}^{n}\textrm{C} - _{k}^{o}\textrm{C}
    其中no分别是总数和奇数的计数。

下面是上述程序的实现:

C++
// C++ implementation to Count of K
// length subsequence whose
// Product is even
  
#include 
using namespace std;
  
int fact(int n);
  
// Function to calculate nCr
int nCr(int n, int r)
{
    if (r > n)
        return 0;
    return fact(n)
           / (fact(r)
              * fact(n - r));
}
  
// Returns factorial of n
int fact(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
  
// Function for finding number
// of K length subsequences
// whose product is even number
int countSubsequences(
    int arr[], int n, int k)
{
    int countOdd = 0;
  
    // counting odd numbers in the array
    for (int i = 0; i < n; i++) {
        if (arr[i] & 1)
            countOdd++;
    }
    int ans = nCr(n, k)
              - nCr(countOdd, k);
  
    return ans;
}
  
// Driver code
int main()
{
  
    int arr[] = { 2, 4 };
    int K = 1;
  
    int N = sizeof(arr) / sizeof(arr[0]);
  
    cout << countSubsequences(arr, N, K);
  
    return 0;
}


Java
// Java implementation to count of K
// length subsequence whose product 
// is even
import java.util.*;
  
class GFG{
      
// Function to calculate nCr
static int nCr(int n, int r)
{
    if (r > n)
        return 0;
    return fact(n) / (fact(r) *
                      fact(n - r));
}
  
// Returns factorial of n
static int fact(int n)
{
    int res = 1;
    for(int i = 2; i <= n; i++)
        res = res * i;
          
    return res;
}
  
// Function for finding number
// of K length subsequences
// whose product is even number
static int countSubsequences(int arr[],
                             int n, int k)
{
    int countOdd = 0;
  
    // Counting odd numbers in the array
    for(int i = 0; i < n; i++)
    {
        if (arr[i] % 2 == 1)
            countOdd++;
    }
    int ans = nCr(n, k) - nCr(countOdd, k);
  
    return ans;
}
  
// Driver code
public static void main(String args[])
{
    int arr[] = { 2, 4 };
    int K = 1;
  
    int N = arr.length;
  
    System.out.println(countSubsequences(arr, N, K));
}
}
  
// This code is contributed by ANKITKUMAR34


Python3
# Python3 implementation to Count of K
# length subsequence whose
# Product is even
  
# Function to calculate nCr
def nCr(n, r):
      
    if (r > n):
        return 0
    return fact(n) // (fact(r) * 
                       fact(n - r))
  
# Returns factorial of n
def fact(n):
      
    res = 1
    for i in range(2, n + 1):
        res = res * i
          
    return res
  
# Function for finding number
# of K length subsequences
# whose product is even number
def countSubsequences(arr, n, k):
      
    countOdd = 0
  
    # Counting odd numbers in the array
    for i in range(n):
        if (arr[i] & 1):
            countOdd += 1;
  
    ans = nCr(n, k) - nCr(countOdd, k);
  
    return ans
      
# Driver code
arr = [ 2, 4 ]
K = 1
  
N = len(arr)
  
print(countSubsequences(arr, N, K))
  
# This code is contributed by ANKITKUAR34


C#
// C# implementation to count of K
// length subsequence whose product 
// is even
using System;
  
class GFG{
      
// Function to calculate nCr
static int nCr(int n, int r)
{
    if (r > n)
        return 0;
          
    return fact(n) / (fact(r) *
                      fact(n - r));
}
  
// Returns factorial of n
static int fact(int n)
{
    int res = 1;
    for(int i = 2; i <= n; i++)
        res = res * i;
          
    return res;
}
  
// Function for finding number
// of K length subsequences
// whose product is even number
static int countSubsequences(int []arr,
                             int n, int k)
{
    int countOdd = 0;
  
    // Counting odd numbers in the array
    for(int i = 0; i < n; i++)
    {
        if (arr[i] % 2 == 1)
            countOdd++;
    }
    int ans = nCr(n, k) - nCr(countOdd, k);
  
    return ans;
}
  
// Driver code
public static void Main(String []args)
{
    int []arr = { 2, 4 };
    int K = 1;
  
    int N = arr.Length;
  
    Console.WriteLine(countSubsequences(arr, N, K));
}
}
  
// This code is contributed by Princi Singh


输出:
2