HDU 1028 Ignatius and the Princess III(母函数)

来源:互联网 发布:javahome的作用 知乎 编辑:程序博客网 时间:2024/06/05 04:49
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1028

题意:给定一个数n,将n拆分成一个或几个不大于n的正整数。是这些正整数的和等于n。求出方案数。

分析:构造母函数G(x)=(1+x+x^2+...+x^n)(1+x^2+x^4+...+x^[(n/2)*2])(1+x^3+x^6+...+x^[(n/3)*3])......(1+x^n),展开后x^n的系数就是所求方案数。

代码:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef long long LL;int main(){int n,i,j,k;LL ans;LL t[130],a[130];while(scanf("%d",&n)!=EOF){ans=0;memset(t,0,sizeof(t));memset(a,0,sizeof(a));a[0]=1; for(i=1;i<=n;i++){for(j=0;j<=n;j++)for(k=0;i*k+j<=n;k++)t[i*k+j]+=a[j];for(j=0;j<=n;j++){a[j]=t[j];t[j]=0;}}printf("%lld\n",a[n]);}return 0;} 


0 0