Advertisement
arnobkumarsaha

RAFI'S DP

Mar 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int A[100],n,cnt=0,money;
  4. vector<int>v;
  5. int dp[10][10];
  6. void print()
  7. {
  8. for(int i=0;i<v.size();i++) cout<<v[i]<<' ';
  9. cout<<"\n";
  10. }
  11. void rec(int i,int amount){
  12. if(i>n || amount > money)return ;
  13. if(amount==money)
  14. {
  15. print();
  16. cnt++;
  17. return;
  18. }
  19. v.push_back(A[i]);
  20. rec(i,amount+A[i]);
  21. v.pop_back();
  22. rec(i+1,amount);
  23. }
  24. int my_DP(int i,int amount)
  25. {
  26. if(i>n || amount > money)return 0;
  27. if(dp[i][amount]!=0)return 1;
  28.  
  29. if(amount==money)
  30. {
  31. print();
  32. //cnt++;
  33. return 1;
  34. }
  35. v.push_back(A[i]);
  36. int p=my_DP(i,amount+A[i]);
  37. v.pop_back();
  38. int q=my_DP(i+1,amount);
  39. return dp[i][amount]=p+q;
  40. }
  41. int main()
  42.  
  43. {
  44. cin>>n>>money;
  45. for(int i=1;i<=n;i++) cin>>A[i];
  46.  
  47. cnt=my_DP(1,0);
  48. cout<<cnt;
  49. return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement