Advertisement
Kali_prasad

no.of pairs of subsets with given difference

Jun 2nd, 2022
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #pragma GCC optimize ("O3")
  2. #pragma GCC target ("sse4")
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8. typedef pair<int, int> pii;
  9. typedef pair<string,int> psi;
  10. typedef priority_queue<int> pqmax;
  11. typedef priority_queue<int,vector<int>,greater<int>> pqmin;
  12. typedef unordered_map<int,int> mii;
  13. typedef unordered_map<long long,long long> mll;
  14. typedef unordered_map<string,int> msi;
  15. typedef unordered_map<char,int> mci;
  16. typedef unordered_set<int> si;
  17. typedef unordered_set<long long> sll;
  18. typedef unordered_set<string> ss;
  19. typedef unordered_set<char> sc;
  20. typedef map<int,int> ormii;
  21. typedef map<long long,long long> ormll;
  22. typedef map<string,int> ormsi;
  23. typedef map<char,int> ormci;
  24. typedef set<int> orsi;
  25. typedef set<long long> orsll;
  26. typedef set<string> orss;
  27. typedef set<char> orsc;
  28. typedef vector<int> vi;
  29. typedef vector<string> vs;
  30. typedef vector<char> vc;
  31. typedef vector<ll> vll;
  32. typedef vector<vector<int>> vvi;
  33. typedef vector<vector<string>> vvs;
  34. typedef vector<vector<bool>> vvb;
  35. typedef vector<vector<ll>> vvll;
  36.  
  37. #define FOR(i, a, b) for (auto i=a; i<=(b); i++)
  38. #define FORd(i,b,a) for (int i =b; i >= a; i--)
  39. #define sortinc(v)  sort(v.begin(),v.end())
  40. #define sortdec(v)  sort(v.rbegin(),v.rend())
  41. #define sz(x) (int)(x).size()
  42. #define mp make_pair
  43. #define pb push_back
  44. #define pob pop_back
  45. #define pf push_front
  46. #define pof pop_front
  47. #define fi first
  48. #define se second
  49. #define ins insert
  50.  
  51. const int MOD = 1000000007;
  52. int m(ll k) {return k%MOD;}
  53. //type functions here
  54.  
  55. int solve(vector<int> &A, int diff) {
  56.     int n=A.size();
  57.     int sum=0;
  58.     for(int x:A) sum+=x;
  59.     int B=(sum-diff)/2;
  60.     vvi dp(n+1,vector<int>(B+1,0));
  61.     for(int i=0;i<=B;i++) dp[0][i]=0;
  62.     for(int i=0;i<=n;i++) dp[i][0]=1;
  63.    
  64.     for(int i=1;i<=n;i++){
  65.     for(int j=1;j<=B;j++)
  66.     {
  67.         if(j>=A[i-1])//current element is not exceeded the sum to acheieve
  68.         dp[i][j]=(dp[i-1][j])+(dp[i-1][j-A[i-1]]);
  69.         else//if exceeded we have to solely depend on n-1 element current sum
  70.         dp[i][j]=dp[i-1][j];
  71.  
  72.        
  73.     }
  74.     }
  75.      return dp[n][B];
  76. }
  77.  
  78.  
  79. int main() {
  80.     ios_base::sync_with_stdio(false);
  81.     cin.tie(NULL);
  82.    
  83.     int tc=1;
  84.     //cin>>tc;
  85.     FOR(w,1,tc)
  86.     {
  87.         vi A={1,1,2,3};
  88.         int B=1;
  89.        cout<<solve(A,B)<<endl;//expected 3 subset pairs
  90.     }
  91.     return 0;
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement