hdu 1284 铅笔兑换问题(母函数)
http://acm.hdu.edu.cn/showproblem.php?pid=1284 题意:有1分,2分,3分的钱若干,问组成n(n<=32767)分钱的方案数。 思路:母函数.
需要注意的是多组数据。每次都搞会TLE,可以先预处理出来存到数组里,每次直接调用。如果预处理时间也还是慢的话,可以先跑出来,然后打表。这算一个小tip吧2333
/* ***********************************************
Author :111qqz
Created Time :2016年02月27日 星期六 16时10分40秒
File Name :code/hdu/1284.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=4E4+7;
7int tmp[N],a[N];
8int n;
9int main()
10{
11 #ifndef ONLINE_JUDGE
12 freopen("code/in.txt","r",stdin);
13 #endif
1 n = 32767;
2 ms(a,0);
3 ms(tmp,0);
4 for ( int i = 0 ; i <= n ; i++)
5 {
6 a[i] = 1;
7 }
1 for ( int i = 2 ; i <= 3 ; i++)
2 {
3 for ( int j = 0 ; j <= n ; j++)
4 {
5 for ( int k = 0 ; k+j <= n ; k+=i)
6 {
7 tmp[k+j] += a[j];
8 // cout<<"aa"<<endl;
9 }
10 }
1 for ( int j = 0 ; j <= n ; j++)
2 {
3 a[j] = tmp[j];
4 tmp[j] = 0;
5 }
}
1 while (~scanf("%d",&n))
2 {
3 printf("%d\n",a[n]);
4 }
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}