📜  乘积为合数的所有子序列的计数

📅  最后修改于: 2021-09-03 13:40:50             🧑  作者: Mango

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

方法:用于查找此类子序列计数的方法与本文中使用的方法类似。此外,该方法可以稍微调整以获得其乘积为质数的子序列的计数。
为了解决上面提到的问题,我们必须找到非空子序列的总数,并减去乘积不是合数的子序列。乘积不是合数的 3 种可能情况是:

  • 1 的任何非空组合是
  • 任何长度为 1 的由质数组成的子序列,该质数基本上是
  • 非空 1 与素数的组合是

下面是上述方法的实现:

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


Javascript


输出:
2

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