[HDU 1028] Ignatius and the Princess III 母函数

来源:互联网 发布:java服务器开发书籍 编辑:程序博客网 时间:2024/06/08 07:55

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

题意:输入一个数 n,求[1, n]中选任意几个数加起来等于 n 的方案有多少种。

思路:母函数 (1 + x^1 + x^2 + x^3 + … + x^n)*(1 + x^2 + x^4 + x^6 +…+ x^(n/2))……(1 + x^n)

#include <cstdio>#include <cstring>#include <iostream>using namespace std;int nex[125],temp[125];int main(){    int n, m;    while(cin>>n>>m)        cout<<gcd(n, m)<<endl;    while(cin>>n){        memset(nex, 0, sizeof(nex));        memset(temp, 0, sizeof(temp));        for(int i = 0; i <= n; i++){            nex[i] = 1;        }        for(int i = 2; i <= n; i++){            for(int j = 0; j <= n; j++){                for(int k = 0; k <= n &&  j+k <= n; k += i){                    temp[j+k] += nex[j];                }            }            for(int j = 0; j <= n; j++){                nex[j] = temp[j];                temp[j] = 0;            }        }        cout<<nex[n]<<endl;    }    return 0;}
0 0