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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
/* *********************************************** Author :111qqz Created Time :2016年02月26日 星期五 12时19分21秒 File Name :code/hdu/2069.cpp ************************************************ */ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <cmath> #include <cstdlib> #include <ctime> #define fst first #define sec second #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define ms(a,x) memset(a,x,sizeof(a)) typedef long long LL; #define pi pair < int ,int > #define MP make_pair using namespace std; const double eps = 1E-8; const int dx4[4]={1,0,0,-1}; const int dy4[4]={0,-1,1,0}; const int inf = 0x3f3f3f3f; const int N=255; int n; int a[N][105],tmp[N][105]; //a[i][j]表示j个硬币构成i元钱的方案数 int s[10]={0,1,5,10,25,50}; int main() { #ifndef ONLINE_JUDGE freopen("code/in.txt","r",stdin); #endif while (~scanf("%d",&n)) { ms(tmp,0); ms(a,0); //多组数据,a数组也要记得初始化 for ( int i = 0 ; i <= min(n,100) ; i++) { a[i][i] = 1; } for ( int i = 2 ; i <= 5 ; i++) { for (int j = 0 ; j <= n ; j++) { for ( int k = 0 ; k*s[i]+j<= n ; k++) { for ( int z = 0 ; z+k <=100 ; z++) { tmp[j+k*s[i]][z+k]+=a[j][z]; } } } for ( int j = 0 ; j <= n ; j++) { for ( int z = 0 ; z <= 100 ; z++) { a[j][z] = tmp[j][z]; tmp[j][z] = 0 ; } } } int ans = 0; for ( int z = 0 ; z <= 100 ; z++) { if (a[n][z]==0) continue; // cout<<z<<" "<<a[n][z]<<endl; ans += a[n][z]; } printf("%d\n",ans); } #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; } |
说点什么
您将是第一位评论人!