hdu 1028 Ignatius and the Princess III(整数拆分,母函数模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1028 题意:求整数拆分数。 思路:母函数模板题。关于母函数的学习:http://www.cnblogs.com/syxchina/archive/2011/07/07/2197205.html http://www.cppblog.com/tanky-woo/archive/2010/08/02/121969.html
具体解释见代码注释。
1/* ***********************************************
2Author :111qqz
3Created Time :2016年02月25日 星期四 21时53分43秒
4File Name :code/hdu/1028.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=125;
34int n;
35int a[N],tmp[N]; //a[i]表示每两个表达式运算时候的前一个表达式的x^i的系数,以及最后的表达式的x^i的系数
36 //tmp[i]是每次运算临时存储指数用
37int main()
38{
39 #ifndef ONLINE_JUDGE
40 freopen("code/in.txt","r",stdin);
41 #endif
42
43 while (~scanf("%d",&n)) //第i个表达式的样子为:(1+x^i,+x^(2*i),....) 表示有一个i,两个i......
44 {
45 for ( int i = 0 ; i <= n ; i++) //所以指数都非负,所以最多有n个表达式对答案有贡献。
46 {
47 a[i] = 1;
48 tmp[i] = 0 ;
49 }
50//每次只考虑两个表达式的运算,把运算结果放在第一个表达式里。
51 for ( int i = 2 ; i <= n ; i++) //i表示第i个表达式
52 {
53 for ( int j = 0 ; j <= n ; j++) //j表示第一个表达式的第j个变量
54 {
55 for ( int k = 0 ; k+j<= n ; k+=i) //k表示第i个表达式的指数,0,i,2*i,3*i...
56 {
57 tmp[j+k] += a[j];
58 }
59 }
60
61 for ( int j = 0 ; j <= n ; j++)
62 {
63 a[j] = tmp[j];
64 tmp[j] = 0 ;
65 }
66 }
67
68 // for ( int i = 0 ; i <= n ; i++) cout<<"i:"<<i<<" "<<a[i]<<endl;
69
70 printf("%d\n",a[n]);
71 }
72
73 #ifndef ONLINE_JUDGE
74 fclose(stdin);
75 #endif
76 return 0;
77}