The cows have not only created their own government but they have chosen to create their own money system. In their own rebellious way, they are curious about values of coinage. Traditionally, coins come in values like 1, 5, 10, 20 or 25, 50, and 100 units, sometimes with a 2 unit coin thrown in for good measure.
The cows want to know how many different ways it is possible to dispense a certain amount of money using various coin systems. For instance, using a system of {1, 2, 5, 10, ...} it is possible to create 18 units several different ways, including: 18x1, 9x2, 8x2+2x1, 3x5+2+1, and many others.
Write a program to compute how many ways to construct a given amount of money using supplied coinage. It is guaranteed that the total will fit into both a signed long long (C/C++) and Int64 (Free Pascal).
The number of coins in the system is V (1 <= V <= 25).
The amount money to construct is N (1 <= N <= 10,000).
Line 1: | Two integers, V and N |
Lines 2..: | V integers that represent the available coins (no particular number of integers per line) |
3 10 1 2 5
A single line containing the total number of ways to construct N money units using V coins.
10
题解:f[i]+=f[i-c[j]]; 当只有一种货币时只要能取的值方法数均为1,当多种货币时每增加一种,只要在原先的基础上加上能达到面额的方法数即可
f[i] 的方法数等于不用此种货币的方法数,加上用此种货币后的方法数。
/* ID:nealgav1 PROG:money LANG:C++ */ #include<fstream> #include<cstring> using namespace std; const int mm=11000; ifstream cin("money.in"); ofstream cout("money.out"); long long f[mm]; int cost[33]; int main() { int n,m; cin>>n>>m; for(int i=0;i<n;i++) cin>>cost[i]; memset(f,0,sizeof(f)); f[0]=1; for(int i=0;i<n;i++) { for(int j=cost[i];j<=m;j++) f[j]+=f[j-cost[i]]; } cout<<f[m]<<endl; }
USER: Neal Gavin Gavin [nealgav1] TASK: money LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 3420 KB] Test 2: TEST OK [0.000 secs, 3420 KB] Test 3: TEST OK [0.000 secs, 3420 KB] Test 4: TEST OK [0.000 secs, 3420 KB] Test 5: TEST OK [0.000 secs, 3420 KB] Test 6: TEST OK [0.000 secs, 3420 KB] Test 7: TEST OK [0.000 secs, 3420 KB] Test 8: TEST OK [0.000 secs, 3420 KB] Test 9: TEST OK [0.000 secs, 3420 KB] Test 10: TEST OK [0.000 secs, 3420 KB] Test 11: TEST OK [0.000 secs, 3420 KB] Test 12: TEST OK [0.000 secs, 3420 KB] Test 13: TEST OK [0.000 secs, 3420 KB] All tests OK.Your program ('money') produced all correct answers! This is your submission #2 for this problem. Congratulations!
Here are the test data inputs:
------- test 1 ---- 1 1 1 ------- test 2 ---- 3 10 1 2 5 ------- test 3 ---- 10 100 1 2 3 4 5 6 7 8 9 10 ------- test 4 ---- 25 1000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25 ------- test 5 ---- 25 9999 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 25000 26 ------- test 6 ---- 25 9999 346 130 982 1090 1656 7117 7595 6415 2948 1126 9004 4558 3571 2879 8492 1360 5412 6721 2463 5047 7119 1441 7190 3985 1214 ------- test 7 ---- 25 999 7843 4639 1653 3876 5457 2152 2524 2412 7461 3564 4634 7717 5947 4056 1048 6123 1757 5964 1137 3094 671 5869 2719 631 3563 ------- test 8 ---- 25 100 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 ------- test 9 ---- 25 100 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 ------- test 10 ---- 5 10000 5 8 13 21 34 ------- test 11 ---- 17 500 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ------- test 12 ---- 8 10000 1 2 3 4 5 6 20 25 ------- test 13 ---- 17 2000 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27Keep up the good work!