HDU1028 Ignatius and the Princess III【母函数】【完全背包】

来源:互联网 发布:手机可以做淘宝客服吗 编辑:程序博客网 时间:2024/05/22 03:20

题目链接:

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


题目大意:

给定正整数N,定义N = a[1] + a[2] + a[3] + … + a[m],a[i] > 0,1 <= m <= N。

对于给定的正整数N,问:能够找出多少种这样的等式?


思路:

对于N = 4,

4 = 4;

4 = 3 + 1;

4 = 2 + 2;

4 = 2 + 1 + 1;

4 = 1 + 1 + 1 + 1。

共有5种。N=4时,结果就是5。其实就是整数分解问题,可写出母函数

g(x) = (1+x+x^2+x^3+…)*(1+x^2+x^4+…)*(1+x^3+…)*(1+x^4+…)

直接套用母函数模板http://blog.csdn.net/lianai911/article/details/45567595

出解即可。

其实,用完全背包也可以做,附上代码。


AC代码:

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;int c1[130],c2[130];int main(){    int N;    while(cin >> N)    {        for(int i = 0; i <= N; ++i)        {            c1[i] = 1;            c2[i] = 0;        }        for(int i = 2; i <= N; ++i)        {            for(int j = 0; j <= N; ++j)                for(int k = 0; j+k <= N; k += i)                    c2[j+k] += c1[j];            for(int j = 0; j <= N; ++j)            {                c1[j] = c2[j];                c2[j] = 0;            }        }        cout << c1[N] << endl;    }    return 0;}

#include<stdio.h>#include<string.h>int dp[200];int main(){    dp[0] = 1;    for(int i = 1; i <= 120; i++)    {        for(int j = i; j <= 120; j++)        {            dp[j] += dp[j-i];        }    }    int n;    while(~scanf("%d",&n))    {        printf("%d\n",dp[n]);    }    return 0;}


0 0
原创粉丝点击