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!"
"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
题意即给你一个整数,问你有多少种拆分方法。
可以先想一想搜索该怎么做,我感觉下面的方法本质还是搜索
先说一下普通的母函数:
G(x)=(1+x1+x2+...+xn)(1+x2+x4...+xn*2)...(1+x1*i+x2*i...+xn*i)。。。(对应该题)
在这里不要用普通的函数的思想来理解,这里的x表示一个单位量,指数即这个单位量的个数,将这个式子的括号消去,即一个个乘起来,最后得到每一项的系数就是对应的方案数(如6x5,就表示组成5的有6种)。为什么呢?再看看。这里每一个括号代表着一个东西,括号里面就代表着对这个括号的选取,如第一个括号,1(x0)代表不选,x1表示选一个该物品得到的量,x2表示选2个该物品...(有没有觉得)
再分析本题,答案即xN的系数。第一个括号表示对数字1的选取,第二个括号对数字2的选取。。。
代码:
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<iostream>
4
5 using namespace std;
6
7 long long a[125]={0},b[125]; //存储对应指数的系数就行了
8 int main()
9 {
10 for(int i=0;i<=121;i++)
11 a[i]=1;
12 for(int i=2;i<=121;i++)
13 {
14 for(int t=0;t<=121;t++) //注意
15 b[t]=a[t];
16
17 for(int j=1;j*i<=121;j++)
18 for(int k=0;j*i+k<=121;k++)
19 a[j*i+k]+=b[k];
20 }
21 int n;
22 while(~scanf("%d",&n))
23 {
24 printf("%lld\n",a[n]);
25 }
26 return 0;
27 }