📌  相关文章
📜  其乘积为复合数的所有子序列的计数

📅  最后修改于: 2021-04-25 01:09:36             🧑  作者: Mango

给定一个数组arr [] ,任务是从给定数组中找到非空子序列的数量,以使子序列的乘积为一个复合数。

例子:

方法:用于查找此类子序列计数的方法与本文中使用的方法相似。同样,可以稍微调整该方法以获得乘积为素数的子序列数。

为了解决上述问题,我们必须找到非空子序列的总数,并减去其乘积不是合成数的子序列。产品不是整数的3种可能情况是:

下面是上述方法的实现:

C++
// C++ implementation to count all
// subsequence whose product
// is Composite number
  
#include 
using namespace std;
  
// Function to check whether a
// number is prime or not
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i < n; i++)
        if (n % i == 0)
  
            return false;
  
    return true;
}
  
// Function to find number of subsequences
// whose product is a composite number
int countSubsequences(int arr[], int n)
{
    // Find total non empty subsequence
    int totalSubsequence = pow(2, n) - 1;
  
    int countPrime = 0, countOnes = 0;
  
    // Find count of prime number and ones
    for (int i = 0; i < n; i++) {
        if (arr[i] == 1)
            countOnes++;
        else if (isPrime(arr[i]))
            countPrime++;
    }
  
    int compositeSubsequence;
  
    // Calculate the non empty one subsequence
    int onesSequence = pow(2, countOnes) - 1;
  
    // Find count of composite subsequence
    compositeSubsequence
        = totalSubsequence - countPrime
          - onesSequence
          - onesSequence * countPrime;
  
    return compositeSubsequence;
}
  
// Driver code
int main()
{
  
    int arr[] = { 2, 1, 2 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << countSubsequences(arr, n);
  
    return 0;
}


Java
// Java implementation to count all
// subsequence whose product
// is Composite number
import java.util.*;
class GFG{
  
// Function to check whether a
// number is prime or not
static boolean isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i < n; i++)
        if (n % i == 0)
  
            return false;
  
    return true;
}
  
// Function to find number of subsequences
// whose product is a composite number
static int countSubsequences(int arr[], int n)
{
    // Find total non empty subsequence
    int totalSubsequence = (int)(Math.pow(2, n) - 1);
  
    int countPrime = 0, countOnes = 0;
  
    // Find count of prime number and ones
    for (int i = 0; i < n; i++) 
    {
        if (arr[i] == 1)
            countOnes++;
        else if (isPrime(arr[i]))
            countPrime++;
    }
  
    int compositeSubsequence;
  
    // Calculate the non empty one subsequence
    int onesSequence = (int)(Math.pow(2, countOnes) - 1);
  
    // Find count of composite subsequence
    compositeSubsequence = totalSubsequence - 
                                 countPrime -
                               onesSequence -
                               onesSequence * 
                               countPrime;
  
    return compositeSubsequence;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 2, 1, 2 };
  
    int n = arr.length;
  
    System.out.print(countSubsequences(arr, n));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation to count 
# all subsequence whose product
# is composite number
  
# Function to check whether 
# a number is prime or not
def isPrime(n):
      
    if (n <= 1):
        return False;
          
    for i in range(2, n):
        if (n % i == 0):
            return False;
  
    return True;
  
# Function to find number of subsequences
# whose product is a composite number
def countSubsequences(arr, n):
      
    # Find total non empty subsequence
    totalSubsequence = (int)(pow(2, n) - 1);
      
    countPrime = 0;
    countOnes = 0;
  
    # Find count of prime number and ones
    for i in range(n):
        if (arr[i] == 1):
            countOnes += 1;
              
        elif (isPrime(arr[i])):
            countPrime += 1;
  
    compositeSubsequence = 0;
  
    # Calculate the non empty one subsequence
    onesSequence = (int)(pow(2, countOnes) - 1);
  
    # Find count of composite subsequence
    compositeSubsequence = (totalSubsequence - 
                                  countPrime - 
                                onesSequence - 
                                onesSequence * 
                                  countPrime);
  
    return compositeSubsequence;
  
# Driver code
if __name__ == '__main__':
      
    arr = [ 2, 1, 2 ];
    n = len(arr);
  
    print(countSubsequences(arr, n));
  
# This code is contributed by 29AjayKumar


C#
// C# implementation to count all
// subsequence whose product
// is Composite number
using System;
class GFG{
  
// Function to check whether a
// number is prime or not
static bool isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i < n; i++)
        if (n % i == 0)
  
            return false;
  
    return true;
}
  
// Function to find number of subsequences
// whose product is a composite number
static int countSubsequences(int []arr, int n)
{
    // Find total non empty subsequence
    int totalSubsequence = (int)(Math.Pow(2, n) - 1);
  
    int countPrime = 0, countOnes = 0;
  
    // Find count of prime number and ones
    for (int i = 0; i < n; i++) 
    {
        if (arr[i] == 1)
            countOnes++;
        else if (isPrime(arr[i]))
            countPrime++;
    }
  
    int compositeSubsequence;
  
    // Calculate the non empty one subsequence
    int onesSequence = (int)(Math.Pow(2, countOnes) - 1);
  
    // Find count of composite subsequence
    compositeSubsequence = totalSubsequence - 
                                 countPrime -
                               onesSequence -
                               onesSequence * 
                                 countPrime;
  
    return compositeSubsequence;
}
  
// Driver code
public static void Main()
{
    int []arr = { 2, 1, 2 };
  
    int n = arr.Length;
  
    Console.Write(countSubsequences(arr, n));
}
}
  
// This code is contributed by Nidhi_biet


输出:
2