📌  相关文章
📜  恰好由 K 个素数组成的子序列的计数

📅  最后修改于: 2021-09-03 04:14:03             🧑  作者: 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


Javascript


输出:
32

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