Advertisement
milon34

Coin change/(dp)

Jan 16th, 2023
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. //problem link:https://lightoj.com/problem/coin-change-i
  2.  
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. #define ll long long int
  6. int n, k;
  7. const ll mod = 100000007;
  8. const ll mx = 1e3 + 5;
  9. ll dp[55][mx];
  10. vector<ll> a;
  11. ll c[25];
  12. ll solve(int i, int sum) {
  13.   if (i == a.size() || sum <= 0) {
  14.     return (sum == 0);
  15.   }
  16.   if (dp[i][sum] != -1) {
  17.     return dp[i][sum];
  18.   }
  19.   ll res = 0;
  20.   for (int m = 0; m <= c[i]; m++) {
  21.     if (sum - (m * a[i]) >= 0) {
  22.       res += solve(i + 1, sum - (m * a[i]));
  23.     } else {
  24.       break;
  25.     }
  26.   }
  27.   return dp[i][sum] = res % mod;
  28. }
  29. int main() {
  30.   ios_base::sync_with_stdio(false);
  31.   cin.tie(0);
  32.   cout.tie(0);
  33.   memset(dp, -1, sizeof(dp));
  34.   int tt, s = 1;
  35.   cin >> tt;
  36.   while (tt--) {
  37.     cin >> n >> k;
  38.     for (int i = 0; i < n; i++) {
  39.       int x;
  40.       cin >> x;
  41.       a.push_back(x);
  42.     }
  43.     for (int i = 0; i < n; i++) {
  44.       cin >> c[i];
  45.     }
  46.     cout << "Case " << s++ << ": " << solve(0, k) % mod << "\n";
  47.     a.clear();
  48.     memset(c, 0, sizeof(c));
  49.   }
  50.   return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement