↓ Skip to main content
  1. Posts/

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

·401 words·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

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}

Related

hdu 2082 找单词 (母函数)

·406 words·1 min
http://acm.hdu.edu.cn/showproblem.php?pid=2082 题意:26个字母,第i个字母有x[i]个,价值为i.问能组成多少个价值不超过50的单词(注意这里的单词只考虑字母的组成,不考虑字母之间的顺序) 思路:母函数。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2016年02月27日 星期六 15时56分31秒 4File Name :code/hdu/2082.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=1E4; 34LL a[N],tmp[N]; 35int x[N]; 36int n; 37int main() 38{ 39 #ifndef ONLINE_JUDGE 40 freopen("code/in.txt","r",stdin); 41 #endif 42 43 44 ios::sync_with_stdio(false); 45 int T; 46 cin>>T; 47 while (T--) 48 { 49 for ( int i = 1; i <= 26 ; i ++) cin>>x[i]; 50 51 ms(tmp,0); 52 ms(a,0); 53 for ( int i = 0 ; i <= x[1]; i++) 54 { 55 a[i] = 1; 56 } 57 // cout<<"aaa"<<endl; 58 int cur = x[1]*1; 59 for ( int i = 2 ; i <= 26 ; i++) 60 { 61 for ( int j = 0 ; j<= cur ; j++) 62 { 63 for ( int k = 0 ; k <= x[i] ; k++) 64 { 65 tmp[j+i*k]+=a[j]; 66 } 67 } 68 69 cur += x[i]*i; 70 71 // cout<<"bbb:"<<endl; 72 73 for ( int j = 0 ; j <= cur ; j++) 74 { 75 a[j] = tmp[j]; 76 tmp[j] = 0 ; 77 } 78 } 79 // cout<<"asdsad"<<endl; 80 LL ans = 0LL ; 81 for ( int j = 1 ; j <= 50 ; j++) 82 { 83 ans += a[j]; 84 } 85 cout<<ans<<endl; 86 } 87 88 #ifndef ONLINE_JUDGE 89 fclose(stdin); 90 #endif 91 return 0; 92}

BC #70 B || hdu 5616 Jam's balance (母函数)

·436 words·1 min
http://acm.hdu.edu.cn/showproblem.php?pid=5616 题意:有n个(n<=20)砝码,第i个重量为w[i],给出m个查询,每个查询一个重量,问这个重量能否被称量出。 思路:暴力(没美感),01背包(不会),母函数(瞬间成了傻逼题)和这题很像 hdu1709 balance hdu1709解题报告

hdu 2152 Fruit (母函数)

·456 words·1 min
http://acm.hdu.edu.cn/showproblem.php?pid=2152 题意:中文题目,大概是说有n(<=100)种水果,第i种至少拿l[i]个,最多拿r[i]个,现在挑选m种水果组成一个果盘,问方案数。 思路:母函数,之前的题目都是只对上界有限制,其实对下界有限制是一样的。以及。。。一开始以为是拿100元买。。。后来发现是“一打百元大钞”23333 其实再出难点可以对个数以及钱数都有限制。。。。

hdu 2069 Coin Change(母函数)

·564 words·2 mins
http://acm.hdu.edu.cn/showproblem.php?pid=2069 题意:有1,5,10,25,50面值的硬币若干,问组成n元钱有多少种不同的方案。一个额外的要求是硬币的总是不能超过100.(那句 your program should be able to handle up to 100 coins.真的是这个意思。。。?感觉好坑。。。)

hdu 1709 The Balance (母函数)

·482 words·1 min
http://acm.hdu.edu.cn/showproblem.php?pid=1709 题意:有n个砝码,第i个的重量为w[i],问从1到sum(所有砝码的重量之和)那些重量无法称量。(所有质量都是整数) 思路:母函数。 一个砝码可以看做有三种状态,放,放左边(+),放右边(-)