📌  相关文章
📜  计算满足给定条件的子集

📅  最后修改于: 2021-04-21 21:48:39             🧑  作者: Mango

给定一个数组arr []和一个整数x ,任务是计算arr []的所有子集(单独地)可被x整除的子集总数。

例子:

方法:要选择所有子集都可以被x整除的子集总和,子集的所有元素都必须可以被x整除。所以,

  • 计算数组中所有可被x整除的元素,并将其存储在变量count中
  • 现在,满足条件的所有可能子集将是2 count – 1

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
#define ll long long int
using namespace std;
  
// Function to return the count of the required sub-sets
ll count(int arr[], int n, int x)
{
  
    // Every element is divisible by 1
    if (x == 1) {
        ll ans = pow(2, n) - 1;
        return ans;
    }
  
    // Count of elements which are divisible by x
    int count = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] % x == 0)
            count++;
    }
  
    ll ans = pow(2, count) - 1;
    return ans;
}
  
// Driver code
int main()
{
    int arr[] = { 2, 4, 3, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 1;
    cout << count(arr, n, x) << endl;
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class solution
{
  
// Function to return the count of the required sub-sets
static long count(int arr[], int n, int x)
{
  
    // Every element is divisible by 1
    if (x == 1) {
        long ans = (long)Math.pow(2, n) - 1;
        return ans;
    }
  
    // Count of elements which are divisible by x
    int count = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] % x == 0)
            count++;
    }
  
    long ans = (long)Math.pow(2, count) - 1;
    return ans;
}
  
// Driver code
public static void main(String args[])
{
    int []arr = { 2, 4, 3, 5 };
    int n = arr.length;
    int x = 1;
    System.out.println(count(arr, n, x));
}
}


Python3
# Python3 implementation of the approach 
  
# Function to return the count of
# the required sub-sets 
def count(arr, n, x) :
  
    # Every element is divisible by 1 
    if (x == 1) :
        ans = pow(2, n) - 1
        return ans; 
      
    # Count of elements which are 
    # divisible by x 
    count = 0
    for i in range(n) : 
        if (arr[i] % x == 0) :
            count += 1
  
    ans = pow(2, count) - 1
    return ans 
  
# Driver code 
if __name__ == "__main__" : 
  
    arr = [ 2, 4, 3, 5 ] 
    n = len(arr)
    x = 1
    print(count(arr, n, x))
  
# This code is contributed by Ryuga


C#
//C# implementation of the approach
  
using System;
  
public class GFG{
      
// Function to return the count of the required sub-sets
static double count(int []arr, int n, int x)
{
    double ans=0;
    // Every element is divisible by 1
    if (x == 1) {
        ans = (Math.Pow(2, n) - 1);
        return ans;
    }
  
    // Count of elements which are divisible by x
    int count = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] % x == 0)
            count++;
    }
  
    ans = (Math.Pow(2, count) - 1);
    return ans;
}
  
// Driver code
      
    static public void Main (){
      
    int []arr = { 2, 4, 3, 5 };
    int n = arr.Length;
    int x = 1;
    Console.WriteLine(count(arr, n, x));
    }
}


PHP


输出:
15