Square Coins
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.
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
题目类型:整数拆分
算法分析:直接使用普通母函数求解即可,注意这里的步长不是每一个递增1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 10000 + 7; const int maxn = 10000 + 66;//注意maxn要大于多项式指数的最高值 int coeff[maxn], temp[maxn];//coeff数组存储多项式各次幂的系数,temp数组存储当前两个多项式相乘的临时结果 const int coin[] = {666, 1 * 1, 2 * 2, 3 * 3, 4 * 4, 5 * 5, 6 * 6, 7 * 7, 8 * 8, 9 * 9, 10 * 10, 11 * 11 , 12 * 12, 13 * 13, 14 * 14, 15 * 15, 16 * 16, 17 * 17}; void Init () { memset (coeff, 0, sizeof (coeff)); memset (temp, 0, sizeof (temp)); } void GenerFunction (long long expnum)//在调用GenerFunction之前一定先调用Init函数 { for (long long i = 0; i <= expnum * coin[1]; i += coin[1]) coeff[i] = 1; for (long long i = 2; i <= 17; i++) { for (long long j = 0; j <= expnum; j++) { for (long long k = 0; k + j <= expnum; k += coin[i]) temp[k+j] += coeff[j]; } for (long long j = 0; j <= expnum; j++) { coeff[j] = temp[j]; temp[j] = 0; } } } int main() { // ifstream cin ("aaa.txt"); long long n; while (cin >> n) { if (n == 0) break; Init (); GenerFunction (n); cout << coeff[n] << endl; } return 0; } |
- « 上一篇:poj3046
- poj3181:下一篇 »