hdu 1284 铅笔兑换问题(母函数)

http://acm.hdu.edu.cn/showproblem.php?pid=1284 题意:有1分,2分,3分的钱若干,问组成n(n<=32767)分钱的方案数。 思路:母函数.

需要注意的是多组数据。每次都搞会TLE,可以先预处理出来存到数组里,每次直接调用。如果预处理时间也还是慢的话,可以先跑出来,然后打表。这算一个小tip吧2333

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年02月27日 星期六 16时10分40秒
 4File Name :code/hdu/1284.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=4E4+7;
34int tmp[N],a[N];
35int n;
36int main()
37{
38	#ifndef  ONLINE_JUDGE 
39	freopen("code/in.txt","r",stdin);
40  #endif
41
42
43	    n = 32767;
44	    ms(a,0);
45	    ms(tmp,0);
46	    for ( int i = 0 ; i <= n ; i++)
47	    {
48		a[i] = 1;
49	    }
50
51
52	    for ( int i = 2 ; i <= 3 ; i++)
53	    {
54		for ( int j = 0 ; j <= n ; j++)
55		{
56		    for ( int k = 0 ; k+j <= n ; k+=i)
57		    {
58			tmp[k+j] += a[j];
59		//	cout<<"aa"<<endl;
60		    }
61		}
62
63		for ( int j = 0  ;  j <= n ; j++)
64		{
65		    a[j] = tmp[j];
66		    tmp[j]  = 0;
67		}
68
69	    }
70
71
72	    while (~scanf("%d",&n))
73	    {
74		printf("%d\n",a[n]);
75	    }
76
77
78  #ifndef ONLINE_JUDGE  
79  fclose(stdin);
80  #endif
81    return 0;
82}