fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. const int N = 100 + 10;
  7. int a[N];
  8. int n, x;
  9. int dp[N][10010];
  10.  
  11. int f(int pos, int sum) {
  12. if(sum == x) return 1; //porque encontre un conjunto de numeros que suman x
  13. if(sum > x) return 0; //porque esta suma no me sirve
  14. if(pos == n) return 0; //llego al final de los elementos sin sumar x
  15.  
  16. if(dp[pos][sum] == -1) {
  17. int ans = 0;
  18. ans = ans + f(pos + 1, sum + a[pos]); //tomo a[pos]
  19. ans = ans + f(pos + 1, sum); //no tomo a[pos]
  20. dp[pos][sum] = ans;
  21. }
  22. return dp[pos][sum];
  23. }
  24.  
  25. int main () {
  26. #ifndef ONLINE_JUDGE
  27. freopen("input.txt", "r", stdin);
  28. freopen("output.txt", "w", stdout);
  29. #endif
  30. cin >> n >> x;
  31. for(int i = 0; i < n; i++)
  32. cin >> a[i];
  33.  
  34. for(int i = 0; i < N; i++)
  35. for(int j = 0; j < 10010; j++)
  36. dp[i][j] = -1;
  37. //memset(dp, -1, sizeof dp);
  38.  
  39. cout << f(0, 0) << "\n";
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 7920KB
stdin
Standard input is empty
stdout
1