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
具体解释见代码注释。
/* ***********************************************
Author :111qqz
Created Time :2016年02月25日 星期四 21时53分43秒
File Name :code/hdu/1028.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=125;
7int n;
8int a[N],tmp[N]; //a[i]表示每两个表达式运算时候的前一个表达式的x^i的系数,以及最后的表达式的x^i的系数
9 //tmp[i]是每次运算临时存储指数用
10int main()
11{
12 #ifndef ONLINE_JUDGE
13 freopen("code/in.txt","r",stdin);
14 #endif
1 while (~scanf("%d",&n)) //第i个表达式的样子为:(1+x^i,+x^(2*i),....) 表示有一个i,两个i......
2 {
3 for ( int i = 0 ; i <= n ; i++) //所以指数都非负,所以最多有n个表达式对答案有贡献。
4 {
5 a[i] = 1;
6 tmp[i] = 0 ;
7 }
8//每次只考虑两个表达式的运算,把运算结果放在第一个表达式里。
9 for ( int i = 2 ; i <= n ; i++) //i表示第i个表达式
10 {
11 for ( int j = 0 ; j <= n ; j++) //j表示第一个表达式的第j个变量
12 {
13 for ( int k = 0 ; k+j<= n ; k+=i) //k表示第i个表达式的指数,0,i,2*i,3*i...
14 {
15 tmp[j+k] += a[j];
16 }
17 }
1 for ( int j = 0 ; j <= n ; j++)
2 {
3 a[j] = tmp[j];
4 tmp[j] = 0 ;
5 }
6 }
// for ( int i = 0 ; i <= n ; i++) cout<<"i:"<<i<<" "<<a[i]<<endl;
printf("%d\n",a[n]);
}
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}