【hdoj_1398】SquareCoins(母函数)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1398
此题采用母函数的知识求解,套用母函数模板即可:
http://blog.csdn.net/ten_sory/article/details/59483762
代码如下
#include<iostream>
using namespace std;
int main()
{
int i,j,m;
int k=17;//一共17个()相乘
int *v = new int[k+1];
int *s = new int[k+1];
int *e = new int[k+1];
for(i=1;i<=k;i++)
{
v[i] = i*i;
s[i] = 0;
e[i] = 301;//每种硬币的个数是无限个(300足够了)
}
int MAX;
while(1)
{
cin >> MAX;
if(!MAX)
break;
int *a = new int[MAX+1];
int *b = new int[MAX+1];
for(i=0;i<=MAX;i++)
{
a[i] = 0;
b[i] = 0;
}
a[0] = 1;
for(i=1;i<=k;i++)
{
for(j=s[i];j<=e[i]&&j*v[i]<=MAX;j++)
for(m=0;m+j*v[i]<=MAX;m++)
b[m+j*v[i]] += a[m];
for(m=0;m<=MAX;m++)
{
a[m] = b[m];
b[m] = 0;
}
}
cout << a[MAX] << endl;
}
return 0;
}
上述代码,提交可以通过.