Kaidul

uva - 357

Feb 16th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6. #define Range 30000
  7. #define int64 long long
  8. int coin[] = {1, 5, 10, 25, 50};
  9. int make;
  10. int dp[10][Range + 10];
  11.  
  12. int64 call(int i, int64 amount) {
  13.     if(i >= 5) {
  14.         if(amount == 0) return 1;
  15.         else return 0;
  16.     }
  17.  
  18.     if(dp[i][amount] != -1) return dp[i][amount];
  19.  
  20.     int64 ret1 = 0, ret2 = 0;
  21.     if (amount - coin[i] >= 0) ret1 = call(i, amount - coin[i]);
  22.     ret2 = call(i + 1, amount);
  23.     return dp[i][amount] = ret1 + ret2;
  24.  
  25. }
  26.  
  27. int main() {
  28.     memset(dp, -1, sizeof dp);
  29.     int64 result;
  30.     while (cin >> make) {
  31.         result = call(0, make);
  32.         if(result <= 1) cout << "There is only 1 way to produce " <<  make << " cents change.\n";
  33.         else cout << "There are " << result << " ways to produce " << make << " cents change.\n";
  34.     }
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment