nrk009

DP SUBSET

Mar 20th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  4. #define ll long long
  5. #define ld long double
  6. #define pb push_back
  7. #define fe first
  8. #define se second
  9. #define st(x) sort(x.begin(),x.end())
  10. #define rst(x) sort(x.rbegin(), x.rend())
  11. #define all(x) x.begin(),x.end()
  12. long double pi = 3.14159265358979323;
  13.  
  14. const double EPS = 1e-12;
  15. const int N = 1e6 + 5;
  16. const int mod = 998244353;
  17.  
  18.  
  19.  
  20.  
  21. int main()
  22. {
  23.     fast;
  24.     ll n ,val  ;
  25.     cin >> n  >> val;
  26.     ll a[n+1] = {0};
  27.     ll sum = 0 ;
  28.     for(int i = 1 ; i <= n ; i++ )
  29.     {
  30.         cin >> a[i];
  31.         sum += a[i];
  32.     }
  33.  
  34.     ll dp[n+1][sum+1];
  35.        for(int i = 0 ; i <= n ; i++)
  36.     {
  37.         for(int j = 0 ; j <= sum ; j++ )
  38.         {
  39.             if(i == 0)
  40.                 dp[i][j] = 0 ;
  41.             else if(j == 0)
  42.                 dp[i][j] = 1;
  43.  
  44.            
  45.             else if(a[i] == j)
  46.                 dp[i][j] = 1;
  47.             else if(a[i] > j)
  48.                 dp[i][j] = dp[i-1][j];
  49.             else
  50.                 dp[i][j] = dp[i-1][j-a[i]];
  51.  
  52.         }
  53.     }
  54.     cout << dp[n][val] << " ";
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.    
  69.     return 0;
  70.    
  71. }
Add Comment
Please, Sign In to add comment