hdu 2069 Coin Change(母函数)

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.真的是这个意思。。。?感觉好坑。。。)

思路:还是母函数,但是由于有了多硬币总数的限制条件,需要加一维.a[i][j]表示j个硬币组成i元钱的方案数(越来越想dp了) 如果转移的时候需要需要加一层硬币的个数。具体见代码。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年02月26日 星期五 12时19分21秒
 4File Name :code/hdu/2069.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=255;
34int n;
35int a[N][105],tmp[N][105];  //a[i][j]表示j个硬币构成i元钱的方案数
36int s[10]={0,1,5,10,25,50};
37int main()
38{
39	#ifndef  ONLINE_JUDGE 
40	freopen("code/in.txt","r",stdin);
41  #endif
42
43	while (~scanf("%d",&n))
44	{
45
46	    ms(tmp,0);
47	    ms(a,0); //多组数据,a数组也要记得初始化
48	    for ( int i = 0 ; i <= min(n,100) ; i++)
49	    {
50		a[i][i] = 1;                   
51	    }
52
53
54
55	    for ( int i = 2 ; i <= 5 ; i++)
56	    {
57		for (int j = 0 ; j <= n ; j++)
58		{
59		    for ( int k = 0 ; k*s[i]+j<= n ; k++)
60		    {
61			for ( int z = 0 ; z+k <=100 ; z++)
62			{
63			    tmp[j+k*s[i]][z+k]+=a[j][z];
64			}
65		    }
66		}
67
68
69		for ( int j = 0 ; j <= n ; j++)
70		{
71		    for ( int z = 0 ; z <= 100 ; z++)
72		    {
73			a[j][z] = tmp[j][z];
74			tmp[j][z] = 0 ;
75		    }
76		}
77
78
79	    }
80
81	    int ans = 0;
82	    for ( int z = 0 ; z <= 100 ; z++)
83	    {
84		if (a[n][z]==0) continue;
85	//	cout<<z<<"  "<<a[n][z]<<endl;
86
87		ans += a[n][z];
88	    }
89
90	    printf("%d\n",ans);
91	}
92
93  #ifndef ONLINE_JUDGE  
94  fclose(stdin);
95  #endif
96    return 0;
97}