fork(2) download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define modulo ll (1e9 + 7)
  6. #define neig(a, u, e, v) for(int v, e = (a).head[u] ; ~e and (v = (a).to[e], 1) ; e = (a).nxt[e])
  7.  
  8. typedef long long ll;
  9.  
  10. const int N = 2e5 + 9, M = 1e6 + 9, OO = 0x3f3f3f3f;
  11. const ll llOO = 0x3f3f3f3f3f3f3f3f;
  12.  
  13. int main() {
  14. cin.tie(0);
  15. cin.sync_with_stdio(0);
  16.  
  17. int n;
  18. cin >> n;
  19.  
  20. ll x;
  21. cin >> x;
  22.  
  23. int a[n];
  24. for(int i = 0;i < n;i++)
  25. cin >> a[i];
  26.  
  27. int ans = 0; // number of subsets with sum equal to x
  28.  
  29. // the binary representation of i represents the subset
  30. for(int i = 0;i < (1 << n);i++){
  31. ll sum = 0;// subset sum
  32. for(int j = 0;j < n;j++){
  33. // check if the j-th bit of i is 1 (j-th element is included in the subset)
  34. if((i >> j) & 1){
  35. sum += a[j];
  36. }
  37. }
  38.  
  39. if(sum == x)
  40. ans++;
  41. }
  42.  
  43. cout << ans;
  44. }
Success #stdin #stdout 2.01s 5384KB
stdin
Standard input is empty
stdout
Standard output is empty