码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-05-14 23:52:32      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:

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;
}


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

标签:

原文地址:http://blog.csdn.net/lianai911/article/details/45727447

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!