Problem Description
“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!”
Input
The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.
Output
For each test case, you have to output a line contains an integer P which indicate the different equations you have found.
Sample Input
4
10
20
Sample Output
5
42
627
题意就是将给定整数n分成任意整数的和,求分法。
本代码使用了母函数思想。
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <complex>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cassert>
using namespace std;
const int MAXN = 10000;
int main()
{
int n;
while(~scanf("%d",&n)){
int c1[MAXN],c2[MAXN];///c1数组存的是当前得到多项式中每一项前的系数,c2是活动数组,
for(int i=0;i<=n;i++){///第一个循环是将第一个括号中多项式中每一项前系数归1,(1+x+x2+x3+x4+...xn),
c1[i]=1;
c2[i]=0;
}
for(int i=2;i<=n;i++){///代表第二个括号开始到第n个括号
for(int j=0;j<=n;j++)///j代表之前已经得到的多项式的指数,c1[j]代表系数
for(int k=0;k+j<=n;k+=i){///代表当前括号中的指数,因为是第i个括号,所以每次递加i
c2[j+k]+=c1[j]; ///当前括号指数为k,之前多项式指数为j,根据x1*x2=x1+2=x3,所以得到的指数为j+k,系数需要继承c1[j]的系数,同时加上可能第二次得到的系数,比如x1*x3=x2*x2=x4,系数叠加。
}
for(int j=0;j<=n;j++){
c1[j]=c2[j]; ///当前系数在下一个括号前转移到c1数组中,代表已经得到的系数。
c2[j]=0; ///活动数组归0。
}
}
cout<<c1[n]<<endl;
}
return 0;
}
大家还可以看看这篇博客。
http://blog.csdn.net/vsooda/article/details/7975485