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了) 如果转移的时候需要需要加一层硬币的个数。具体见代码。
/* ***********************************************
Author :111qqz
Created Time :2016年02月26日 星期五 12时19分21秒
File Name :code/hdu/2069.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=255;
7int n;
8int a[N][105],tmp[N][105]; //a[i][j]表示j个硬币构成i元钱的方案数
9int s[10]={0,1,5,10,25,50};
10int main()
11{
12 #ifndef ONLINE_JUDGE
13 freopen("code/in.txt","r",stdin);
14 #endif
while (~scanf("%d",&n))
{
1 ms(tmp,0);
2 ms(a,0); //多组数据,a数组也要记得初始化
3 for ( int i = 0 ; i <= min(n,100) ; i++)
4 {
5 a[i][i] = 1;
6 }
1 for ( int i = 2 ; i <= 5 ; i++)
2 {
3 for (int j = 0 ; j <= n ; j++)
4 {
5 for ( int k = 0 ; k*s[i]+j<= n ; k++)
6 {
7 for ( int z = 0 ; z+k <=100 ; z++)
8 {
9 tmp[j+k*s[i]][z+k]+=a[j][z];
10 }
11 }
12 }
1 for ( int j = 0 ; j <= n ; j++)
2 {
3 for ( int z = 0 ; z <= 100 ; z++)
4 {
5 a[j][z] = tmp[j][z];
6 tmp[j][z] = 0 ;
7 }
8 }
}
1 int ans = 0;
2 for ( int z = 0 ; z <= 100 ; z++)
3 {
4 if (a[n][z]==0) continue;
5 // cout<<z<<" "<<a[n][z]<<endl;
ans += a[n][z];
}
printf("%d\n",ans);
}
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}