hdu 2069(母函数)
2017-08-13 20:20
417 查看
Coin Change
[b] Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 20356 Accepted Submission(s): 7154
[/b]
[align=left]Problem Description[/align]Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money.
For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, or two 5-cent coins and one 1-cent coin, or one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent.
Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 100 coins.
[align=left]Input[/align]The input file contains any number of lines, each one consisting of a number ( ≤250 ) for the amount of money in cents.
[align=left]Output[/align]For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.
[align=left]Sample Input[/align]11
26
[align=left]Sample Output[/align][align=left]4[/align][align=left]13[/align][align=left]
[/align][align=left]题目题意:给你N分钱,问你这N分钱能用上面那五种硬币(1,5,10,25,50)替换的总方案数。但是这里有一个限定兑换的硬币的总个数不能超过100个。[/align][align=left]
[/align][align=left]题目分析:这里我们只需要在传统的的母函数上加上一维,来对数量加以限定.[/align][align=left]
[/align]
#include<iostream> #include<cstdio> #include<cstring> #define ll long long using namespace std; const int maxn=300; int c1[maxn][maxn],c2[maxn][maxn];//第二维用来对数量加以限定。 int b[5]={1,5,10,25,50}; int a[5]; int main() { int n; while (scanf("%d",&n)!=EOF) { memset (c1,0,sizeof (c1)); memset (c2,0,sizeof (c2)); memset (a,0,sizeof (a)); if (n==0) { printf("1\n"); continue;}//根据题意0分也是一种方案 for (int i=0;i<5;i++) a[i]=n/b[i];//求出每种硬币的最大数目 for (int i=0;i<=min(100,n);i++) c1[i][i]=1; for (int i=1;i<5;i++) { for (int j=0;j<=n;j++) { for (int k=0;k+j<=n&&k<=a[i]*b[i];k+=b[i]) { for (int l=0;l+k/b[i]<=100;l++)//该循环用来加以判断数量 c2[j+k][l+k/b[i]]+=c1[j][l]; } } for (int k=0;k<=n;k++) { for (int l=0;l<=100;l++) { c1[k][l]=c2[k][l]; c2[k][l]=0; } } } int ans=0; for (int i=0;i<=100;i++) ans+=c1 [i]; cout<<ans<<endl; } return 0; }
[align=left]
[/align][align=left]
[/align][align=left]
[/align][align=left]
[/align][align=left]
[/align]
相关文章推荐
- HDU 2069 母函数模版题
- HDU 2069 Coin Change【带总数限制的母函数】
- HDU 2069 母函数模版题
- (简单母函数进阶版,暴力)hdu 2069 Coin Change
- HDU 2069 母函数模版题
- HDU 2069 母函数
- HDU 2069 母函数模版题
- HDU 2069 母函数
- uva 674 && hdu 2069 coin change(动态规划、母函数)
- HDU 2069 母函数模版题
- HDU 2069 母函数
- 【母函数】HDU-2069 Coin Change
- HDU 2069 母函数模版题
- hdu 2069 普通母函数 +dp
- HDU 2069 母函数模版题
- HDU 2069 母函数模版题
- HDU 2069 母函数模版题
- UVa 674 & hdu 2069 Coin Change (母函数,dp)
- HDU 2069 Coin Change (母函数)
- hdu 2069 Coin Change (母函数)