HDU 1398 Square Coins
Square Coins
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Problem Description
People in Silverland use square coins. Not only they have square shapes but also their values are square numbers. Coins with values of all square numbers up to 289 (=17^2), i.e., 1-credit coins, 4-credit coins, 9-credit coins, ..., and 289-credit coins, are available in Silverland.
There are four combinations of coins to pay ten credits:
ten 1-credit coins,
one 4-credit coin and six 1-credit coins,
two 4-credit coins and two 1-credit coins, and
one 9-credit coin and one 1-credit coin.
Your mission is to count the number of ways to pay a given amount using coins of Silverland.
There are four combinations of coins to pay ten credits:
ten 1-credit coins,
one 4-credit coin and six 1-credit coins,
two 4-credit coins and two 1-credit coins, and
one 9-credit coin and one 1-credit coin.
Your mission is to count the number of ways to pay a given amount using coins of Silverland.
Input
The input consists of lines each containing an integer meaning an amount to be paid, followed by a line containing a zero. You may assume that all the amounts are positive and less than 300.
Output
For each of the given amount, one line containing a single integer representing the number of combinations of coins should be output. No other characters should appear in the output.
Sample Input
2
10
30
0
Sample Output
1
4
27
Source
Recommend
生成函数,问用一些有使用次数限制的数字能组合出某个数字的方案数。
1 #include <cstdio> 2 #include <cstring> 3 4 template <class T> 5 inline void read(T &x) 6 { 7 x = 0; 8 char c = getchar(); 9 while (c < 48)c = getchar(); 10 while (c > 47)x = x*10 + c - 48, c = getchar(); 11 } 12 13 const int siz = 305; 14 const int lim = 300; 15 16 int n; 17 int f[siz]; 18 int s[siz]; 19 int t[siz]; 20 21 signed main(void) 22 { 23 f[0] = 1; 24 25 for (int i = 1; i <= 17; ++i) 26 { 27 memset(s, 0, sizeof s); 28 memset(t, 0, sizeof t); 29 30 for (int j = 0; j*i*i <= lim; ++j) 31 s[j * i * i] = 1; 32 33 for (int j = 0; j <= lim; ++j)if (s[j]) 34 for (int k = 0; k <= lim; ++k)if (f[k]) 35 t[j + k] += s[j] * f[k]; 36 37 memcpy(f, t, sizeof f); 38 } 39 40 while (read(n), n)printf("%d\n", f[n]); 41 }
@Author: YouSiki