Problem Description
In a country there are only 1 cents, 2 cents, 3 cents, and there are many ways to exchange money n coins. Please write a procedure to figure out how many kinds of methods are in common.
Input
Only one positive integer per line n,n less than 32768.
Output
For each input, the output conversion method number.
Sample Input
2934
12553
Sample Output
718831
13137761
What is a parent function.
Suppose 1 points, 3 points, 5 cents each of each coin, you can get:
(1+x) * (1+x^3) * (1+x^5) =1+x+x^3+x^4+x^5+x^6+x^8+x^9
After expansion, a method that can be composed of a value of 1, 3, 4, 5, 6, 8, 9, each
If each of the two pieces, you can get:
(1+x+x^2) * (1+x^3+x^6) * (1+X^5+X^10)
Similarly, if each of the three pieces, you can get:
(1+x+x^2+x^3) * (1+x^3+x^6+x^9) * (1+x^5+x^10+x^15)
So open the double loop, the outer layer of the number of coins to traverse, inside a layer of the number of each of the coins, with an array of the number of methods to record each denomination.
#include <cstring> #include <cstdlib> #include <cstdio> using namespace std
;
const int max=32769;
int Ans[max],tans[max];
int main () {int i,j,n;
for (int i=0;i<max;i++) ans[i]=1;
memset (Tans,0,sizeof (tans)); for (int k=2;k<=3;k++) {for (i=0;i<max;i++) for (j=0;i+j<max;j+=k) Tans[i+j]+=ans
[i];
for (i=0;i<max;i++) {ans[i]=tans[i];
tans[i]=0;
} while (scanf ("%d", &n)!=eof) printf ("%d\n", Ans[n));
return 0; }