题目链接: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=a1+a2+a3+…+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!”
思路
题意很明显了,就是告诉你一个数字,询问有多少种方式组成这个数;
这是经典的整数划分问题,我们可以用递归、dp、母函数等等方法求解;
递归做会T掉,我们这里运用母函数求解;
如果你不了解母函数,以下为推荐的两篇学习母函数的博客
母函数学习
母函数学习2
母函数在我的理解就是把组合问题的加法法则和幂级数的乘幂对应起来,这样就可以解决一些问题;
到最后,指数的大小就是(价值,质量等),系数就是方案数;
当然,他可以优化一下,就是记录每个式子当前的最高次幂和上一次的最高次幂;
可以节省一定的时间;
代码
#include <bits/stdc++.h>
using namespace std;#define N 210int c1[N], c2[N];int main()
{
int n;while(cin >> n){
memset(c1, 0, sizeof(c1));memset(c2, 0, sizeof(c2));c1[0] = 1;for(int i = 0; i < n; i++){
for(int j = 0; j * (i + 1) <= n; j++){
for(int k = 0; k + j * (i + 1) <= n; k++){
c2[k + j * (i + 1)] += c1[k];}}for(int j = 0; j <= n; j++){
c1[j] = c2[j];c2[j] = 0;}}cout << c1[n] << endl;}return 0;
}