Advertisement
newb_ie

UVA - 357

Jul 18th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1.  /*
  2.        _________
  3.       /         \
  4.      /  _     _  \
  5.     /   0     0   \
  6.    |       ||     |
  7.    |              | => HI
  8.     \     -----   /
  9.      \           /
  10.       \_ _ _ _ _/
  11.  */
  12.  
  13. #include<bits/stdc++.h>
  14. using namespace std;
  15. using lli = int64_t;
  16. const int maxN = 30200;
  17. lli grid[5][maxN];
  18. int coins[] = {1,5,10,25,50};
  19. lli solve(int idx,int n){
  20.     if(idx == 5 or n == 0){
  21.         return n == 0;
  22.     }
  23.     if(grid[idx][n] != -1){
  24.         return grid[idx][n];
  25.     }
  26.     lli res = 0;
  27.     for(int i = 0;i <= n; i++){
  28.         if(n - (i * coins[idx]) >= 0){
  29.             res += solve(idx + 1,n - (i * coins[idx]));
  30.         }else{
  31.             break;
  32.         }
  33.     }
  34.     return grid[idx][n] = res;
  35. }
  36. int main(){
  37.     ios::sync_with_stdio(false);
  38.     cin.tie(nullptr);
  39.     memset(grid,-1,sizeof(grid));
  40.     int n;
  41.     while(cin >> n and EOF){
  42.         lli res = solve(0,n);
  43.         if(res == 1){
  44.             cout << "There is only 1 way to produce " << n << " cents change.\n";
  45.             continue;
  46.         }
  47.         cout << "There are " << res << " ways to produce " << n << " cents change.\n";
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement