hdu 2069(母函数)

来源:互联网 发布:java cp classpath 编辑:程序博客网 时间:2024/05/20 16:10

                                                     Coin Change

                                                               Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                                   Total Submission(s): 20356    Accepted Submission(s): 7154


Problem Description
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.
 

Input
The input file contains any number of lines, each one consisting of a number ( ≤250 ) for the amount of money in cents.
 

Output
For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.
 

Sample Input
1126
 

Sample Output
4
13

题目题意:给你N分钱,问你这N分钱能用上面那五种硬币(1,5,10,25,50)替换的总方案数。但是这里有一个限定兑换的硬币的总个数不能超过100个。

题目分析:这里我们只需要在传统的的母函数上加上一维,来对数量加以限定.

#include<iostream>#include<cstdio>#include<cstring>#define ll long longusing 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[n][i];        cout<<ans<<endl;    }    return 0;}






原创粉丝点击