1. 程式人生 > >HDU1028 Ignatius and the Princess III【母函式】【完全揹包】

HDU1028 Ignatius and the Princess III【母函式】【完全揹包】

題目連結:

題目大意:

給定正整數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+…)

出解即可。

其實,用完全揹包也可以做,附上程式碼。

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函式

Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s)

HDU1028 Ignatius and the Princess III函式完全揹包

題目連結: 題目大意: 給定正整數N,定義N = a[1] + a[2] + a[3] + … + a[m],a[i] > 0,1 <= m <= N。 對於給定的正整數N,問:

完全揹包/函式/遞推HDU1028-Ignatius and the Princess III

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1028 Problem Description "Well, it seems the first problem is too easy. I will let you

HDU1028 Ignatius and the Princess III 函數

[1] strong %d fuck any build other main \n Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32

題解報告:hdu 1028 Ignatius and the Princess III函數orDP)

函數 OS fir c++ ali 元素 namespace output 一個數 Problem Description "Well, it seems the first problem is too easy. I will let you know how fool

Ignatius and the Princess III(杭電1028)(函數)

mission des panel mes content nat strong pro accepted Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory L

HDU Ignatius and the Princess III (函數)

tex namespace class .net author def urn rst isp Problem Description "Well, it seems the first problem is too easy. I will let you know h

hdu 1028 Ignatius and the Princess III 函數

input comm show fir case ces panel typedef get Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6553

hdu 1028 Ignatius and the Princess III生成函式

老是想著化簡,實際上O(n^3)就行了…… 寫成生成函式是\( \prod_{i=1}^{n}(1+x^i+2^{2i}+...+x^{ \left \lfloor \frac{n}{i} \right \rfloor }) \),暴力乘即可 #include<iostream> #includ

hdu 1028 Ignatius and the Princess III生成函數

main printf += 生成函數 cpp 化簡 sin ESS n) 老是想著化簡,實際上O(n^3)就行了…… 寫成生成函數是\( \prod_{i=1}^{n}(1+x^i+2^{2i}+...+x^{ \left \lfloor \frac{n}{i} \rig

hdu 1028 Ignatius and the Princess III ( 函式

Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12

HDU 1028 Ignatius and the Princess III dp

cep 大數 style code 代碼 des for each 狀態轉移方程 遞推 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1028 一道經典題,也是算法設計與分析上的一道題,可以用遞推,動態規劃,母函數求解,我用的

hdu 1028 Ignatius and the Princess III —— 整數劃分(生成函數)

int const 整數劃分 pan 題目 algo while php cst 題目:http://acm.hdu.edu.cn/showproblem.php?pid=1028 整數劃分,每個數可以用無限次; 所以構造 f(x) = (1+x+x2+x3+...)(1+

Ignatius and the Princess III HDU - 1028 -生成函式or完全揹包計數

 HDU - 1028  step 1:初始化第一個多項式 也就是 由 1的各種方案 組 成 的多項式 初始化係數為 1。臨時區 temp初始化 為 0 step 2:遍歷後續的n - 1 個 多項式 ,第二重 for  j  代 表 的 存 儲

HDU 1028 Ignatius and the Princess III(DP,整數劃分)

Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2

HDU 1026.Ignatius and the Princess IBFS廣度優先搜尋+優先佇列+前驅記錄8月16

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 17

HDU1027 Ignatius and the Princess II全排列

Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11536 Accepted Submissi

J - Ignatius and the Princess II

() tdi after pretty assume pen inpu def show Now our hero finds the door to the BEelzebub feng5166. He opens the door and finds feng5166

[ACM] hdu 1029 Ignatius and the Princess IV (動歸或hash)

inner other for each sam 代碼 test case break accep bre Ignatius and the Princess IV Time Limit : 2000/1000ms (Java/Other) Memory Li

HDOJ 1026 Ignatius and the Princess I

urn weight col str using n+1 them appear ... Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3