HDU 1398 (母函数)
/*
题目意思是:硬币种类有1^2,2^2,3^2,4^2...17^2,这几种;输入n;求出能够组合成n的组合有多少种;
与hdu1028相似,只是将原来的1,2,3,4,……换成了1^2,2^2,3^2,4^2...…,
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1398
*/
#include <iostream> #include <cstring> using namespace std; int a[305],b[305]; int main() { int n; int c[18]; for(int i = 0; i <= 17; i++) { c[i] = i*i; } while(cin>>n&&n!=0) { for(int i = 0; i <= n; i++) { a[i] = 1; b[i] = 0; } for(int i = 2; i <= 17; i++) { for(int j = 0; j <= n; j++) //枚举已知范围 (前面所有项结果项结果) for(int k = 0; k+j <= n; k+=c[i]) //枚举新增范围(下一项 ) { b[j+k] += a[j]; } for(int j = 0; j <= n; j++) //重置 { a[j] = b[j]; b[j] = 0; } } cout<<a[n]<<endl; } }