hdu 1398 Square Coins/hdu 1028 Ignatius and the Princess III

两道母函数的模板题:

http://acm.hdu.edu.cn/showproblem.php?pid=1398

View Code
#include<iostream>
#include<cstdio>
using namespace std;
int c1[310],c2[310];
int main()
{
int n,i,j,k;
while(scanf("%d",&n),n)
{
for(i=0;i<=n;i++)
{
c1[i]=1;
c2[i]=0;
}
for(i=2;i*i<=n;i++)
{
for(j=0;j<=n;j++)
for(k=0;k+j<=n;k+=i*i)
c2[k+j]+=c1[j];
for(j=0;j<=n;j++)
{
c1[j]=c2[j];
c2[j]=0;
}
}
printf("%d\n",c1[n]);
}
return 0;
}

http://acm.hdu.edu.cn/showproblem.php?pid=1028

View Code
#include<iostream>
#include<cstdio>
using namespace std;
int c1[130],c2[130];
int main()
{
int n,i,j,k;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<=n;i++)
{
c1[i]=1;
c2[i]=0;
}
for(i=2;i<=n;i++)
{
for(j=0;j<=n;j++)
for(k=0;k+j<=n;k+=i)
c2[k+j]+=c1[j];
for(j=0;j<=n;j++)
{
c1[j]=c2[j];
c2[j]=0;
}
}
printf("%d\n",c1[n]);
}
return 0;
}





posted @ 2011-11-26 18:09  LT-blogs  阅读(182)  评论(0编辑  收藏  举报