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

链接:

https://vjudge.net/problem/HDU-1028

题意:

"Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says.

"The second problem is, given an positive integer N, we define an equation like this:
N=a[1]+a[2]+a[3]+...+a[m];
a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!"

思路:

母函数模板题。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>

using namespace std;
typedef long long LL;
const int INF = 1e9;

const int MAXN = 120+10;
int c1[MAXN], c2[MAXN];
int n;

void Init()
{
    c1[0] = 1;
    for (int i = 1;i < MAXN;i++)
    {
        for (int j = 0;j < MAXN;j+=i)
        {
            for (int k = 0;k+j < MAXN;k++)
                c2[j+k] += c1[k];
        }
        for (int j = 0;j < MAXN;j++)
        {
            c1[j] = c2[j];
            c2[j] = 0;
        }
    }
}

int main()
{
    Init();
    while(~scanf("%d", &n))
    {
        printf("%d
", c1[n]);  
    }

    return 0;
}
原文地址:https://www.cnblogs.com/YDDDD/p/11802813.html