lsajkdf

CSES coin combinations 2

Jul 25th, 2023
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #pragma GCC optimize("O3,unroll-loops")
  2. #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. using ll = long long;
  7. using lld = long double;
  8. using ull = unsigned long long;
  9. using vll = vector<ll>;
  10. using pll = pair<ll, ll>;
  11. using vpll = vector<pll>;
  12.  
  13. #ifndef ONLINE_JUDGE
  14. #define debug(x) cerr << #x<<" "; _print(x); cerr << endl;
  15. #else
  16. #define debug(x) ((void)0);
  17. #endif
  18.  
  19. #define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  20. #define loop(i,k,n) for(ll i = k; i < n;i++)
  21. #define MOD 1000000007
  22. #define nl "\n"
  23. #define pb push_back
  24.  
  25.  
  26. vll v;
  27. ll dp[101][1000001];    // such big array not allowed gives runtime error
  28. // is there some way i can make this into dp[2][1000001]
  29. // but still keep this recursive (or not make much changes to recursive code)
  30.  
  31. ll f(ll i, ll j) {
  32.     if(j == 0) return 1;
  33.     if(i == 0) return 0;
  34.     if(dp[i][j] != -1) return dp[i][j];
  35.     ll op1 = ((i-1 >=0) ? f(i-1 , j) : 0);
  36.     ll op2 = ((j-v[i] >= 0) ? f(i , j-v[i]) : 0);
  37.     ll ans = (op1 + op2) % MOD;
  38.     return dp[i][j] = ans;
  39. }
  40.  
  41. void solve(){
  42.     //testing
  43.     ll n, sum; cin >> n >> sum;
  44.     v.assign(n+1, 0);
  45.     memset(dp, -1, sizeof dp);  // dp(index, sum) -> gives no of ways
  46.     loop(i, 1, n+1) cin >> v[i];    // 1 based indexing
  47.     // vector<vll> dp(2, vll(sum + 1));
  48.     // loop(i, 0, n+1) {
  49.     //     loop(j, 0, sum + 1) {
  50.     //         if(j == 0) {
  51.     //             dp[i&1][j] = 1;
  52.     //         }
  53.     //         else if(i == 0) {
  54.     //             // j is non zero here, so no possible way
  55.     //             dp[i&1][j] = 0;
  56.     //         }
  57.     //         else{
  58.     //             ll op1 = 0, op2 = 0;
  59.     //             op1 = (i-1 >=0) ? dp[(i-1)&1][j] : 0;
  60.     //             op2 = (j-v[i] >= 0) ? dp[i&1][j-v[i]] : 0;
  61.     //             dp[i&1][j] = op1 + op2;
  62.     //             dp[i&1][j] %= MOD;
  63.     //         }
  64.     //     }
  65.     // }
  66.     cout << f(n, sum) << nl;
  67. }
  68.  
  69. int main(){
  70.     fastio();
  71.     #ifndef ONLINE_JUDGE
  72.     freopen("input.txt", "r", stdin);
  73.     freopen("output.txt", "w", stdout);
  74.     freopen("error.txt", "w", stderr);
  75.     #endif
  76.  
  77.     int t = 1;
  78.     // cin>>t;
  79.     for(ll i = 1; i <= t; i ++)
  80.     {  
  81.         solve();
  82.     }
  83. }
Add Comment
Please, Sign In to add comment