📜  正好由K个素数组成的子序列数

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

给定一个整数K和一个数组arr [] ,任务是从给定数组中找到子序列的数量,以使每个子序列恰好由K个质数组成。
例子:

方法:要解决上述问题,我们必须找到给定数组中素数的计数。

  1. 设素数为m
  2. 我们可以在m个质数中选择任意K个整数。
  3. 因此,在子序列中选择质数的可能组合为mCk 在子序列中,我们可以添加任意数量的非质数,因为对非质数没有限制,非质数的计数为(N – m)。
  4. 对于子序列中的非素数,我们可以选择(N – m)的任何子集。
  5. 选择大小为(N – m)的所有子集的可能性为pow(2,(m – N))
  6. 为了生成子序列,我们将素数可能性与非素数可能性相乘。

下面是上述方法的实现:

C++
// C++ implementation to find
// the count of subsequences
// which consist exactly K primes
  
#include 
using namespace std;
  
// Returns factorial of n
int fact(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
  
// Function to return total
// number of combinations
int nCr(int n, int r)
{
    return fact(n)
           / (fact(r)
              * fact(n - r));
}
  
// Function check whether a number
// is prime or not
bool isPrime(int n)
{
    // Corner case
    if (n <= 1)
        return false;
  
    // Check from 2 to n-1
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;
  
    return true;
}
  
// Function for finding number of subsequences
// which consists exactly K primes
int countSubsequences(int arr[], int n, int k)
{
    int countPrime = 0;
    for (int i = 0; i < n; i++) {
        if (isPrime(arr[i]))
            countPrime++;
    }
    // if number of primes are less thn k
    if (countPrime < k)
        return 0;
  
    return nCr(countPrime, k)
           * pow(2, (n - countPrime));
}
  
// Driver code
int main()
{
  
    int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
    int K = 3;
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << countSubsequences(arr, n, K);
    return 0;
}


Java
// Java implementation to find the
// count of subsequences which 
// consist exactly K primes
import java.util.*;
  
class GFG{
      
// 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 to return total
// number of combinations
static int nCr(int n, int r)
{
    return fact(n) / (fact(r) *
                      fact(n - r));
}
  
// Function check whether a number
// is prime or not
static boolean isPrime(int n)
{
      
    // Corner case
    if (n <= 1)
        return false;
  
    // Check from 2 to n-1
    for(int i = 2; i < n; i++)
        if (n % i == 0)
            return false;
  
    return true;
}
  
// Function for finding number of subsequences
// which consists exactly K primes
static int countSubsequences(int arr[], 
                             int n, int k)
{
    int countPrime = 0;
    for(int i = 0; i < n; i++)
    {
        if (isPrime(arr[i]))
            countPrime++;
    }
      
    // If number of primes are less thn k
    if (countPrime < k)
        return 0;
  
    return nCr(countPrime, k) *
          (int)Math.pow(2, (n - countPrime));
}
  
// Driver code
public static void main(String args[])
{
  
    int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
    int K = 3;
    int n = arr.length;
  
    System.out.println(countSubsequences(arr, n, K));
}
}
  
// This code is contributed by ANKITKUMAR34


Python3
# Python3 implementation to find the
# count of subsequences which consist
# exactly K primes
  
# Returns factorial of n
def fact(n):
      
    res = 1;
    for i in range(2, n + 1):
        res = res * i
          
    return res
  
# Function to return total
# number of combinations
def nCr(n, r):
      
    return (fact(n) // (fact(r) * 
                        fact(n - r)))
  
# Function check whether a number
# is prime or not
def isPrime(n):
      
    # Corner case
    if (n <= 1):
        return False;
  
    # Check from 2 to n-1
    for i in range(2, n):
        if (n % i == 0):
            return False
  
    return True
  
# Function for finding number of subsequences
# which consists exactly K primes
def countSubsequences(arr, n, k):
      
    countPrime = 0
    for i in range(n):
        if (isPrime(arr[i])):
            countPrime += 1
              
    # If number of primes are less than k
    if (countPrime < k):
        return 0
  
    return (nCr(countPrime, k) * 
    pow(2, (n - countPrime)))
  
# Driver code
arr = [ 1, 2, 3, 4, 5, 6, 7 ]
K = 3
n = len(arr)
  
print(countSubsequences(arr, n, K))
  
# This code is contributed by ANKITKUMAR34


C#
// C# implementation to find the
// count of subsequences which 
// consist exactly K primes
using System;
  
class GFG{
      
// 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 to return total
// number of combinations
static int nCr(int n, int r)
{
    return fact(n) / (fact(r) *
                      fact(n - r));
}
  
// Function check whether a number
// is prime or not
static bool isPrime(int n)
{
      
    // Corner case
    if (n <= 1)
        return false;
  
    // Check from 2 to n-1
    for(int i = 2; i < n; i++)
        if (n % i == 0)
            return false;
  
    return true;
}
  
// Function for finding number of subsequences
// which consists exactly K primes
static int countSubsequences(int []arr, 
                             int n, int k)
{
    int countPrime = 0;
    for(int i = 0; i < n; i++)
    {
        if (isPrime(arr[i]))
            countPrime++;
    }
      
    // If number of primes are less than k
    if (countPrime < k)
        return 0;
  
    return nCr(countPrime, k) *
          (int)Math.Pow(2, (n - countPrime));
}
  
// Driver code
public static void Main(String []args)
{
  
    int []arr = { 1, 2, 3, 4, 5, 6, 7 };
    int K = 3;
    int n = arr.Length;
  
    Console.WriteLine(countSubsequences(arr, n, K));
}
}
  
// This code is contributed by gauravrajput1


输出:
32