题目:http://acm.hdu.edu.cn/showproblem.php?pid=1398
题意:有货币价值1,4,9……17^2,数量不限,问组成n元的方案数。
思路:
1 对应母函数G(1):1+x+x^2+x^3+……
4 对应母函数G(4):1+x^4+x^8+x^12+……
G(1)*G(4)得到:1+x+x^2+x^3+2(x^4)+2(x^5)+2(x^6)+2(x^7)+3(x^8)+3(x^9)+3(x^10)+……
当前,x^n对应的系数即为用1和4组成和为n的方案数
G(1)*G(4)*G(9)*......*G(17^2)得到的最后的函数,x^n对应的系数即为用1,4,9……17^2 组成和为n的方案数
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#define INF 0x7fffffff
#define MOD 1000000007
using namespace std;
typedef long long ll;
ll a[5][305];
int main()
{
#ifdef LOCAL
freopen("data.in", "r", stdin);
#endif
int N = 300;
for(int i = 0; i <= N; i++)
a[1][i] = 1;
for(int i = 2; i <= 17; i++)
{
int c = i * i;
for(int j = 0; j <= N; j++)
a[i % 2][j] = a[(i + 1) % 2][j];
for(int j = 0; j <= N; j++)
for(int k = c; k <= N; k += c)
a[i % 2][j + k] += a[(i + 1) % 2][j];
}
int n;
while(scanf("%d", &n) != EOF && n)
{
printf("%I64d\n", a[1][n]);
}
return 0;
}