HDU 2069 Coin Change

Coin Change

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


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
 
 
11 26
 

Sample Output
 
 
4 13
 

Author
Lily
 

Source
 

题意:问总数为n的钱币可由{1,5,10,25,50}这几种钱币组成的方法数;总钱币的数目不得超过100;

思路:普通型母函数:G(x)=(1+x+x^2+x^3+......+x^num1)(1+x^5+x^10+x^15+.....x^num2)......;

           由于要求总数小于100,所以加一层循环限制并且用二维数组dp[i][j]保存,意思为组成i钱币用了j个硬币的方法数;、

        之后直接上母函数模板,最后用一维数组累加记录即可;

下面附上我的代码:

#include<bits/stdc++.h>
using namespace std;
const int M=255;
int c1[M][101],c2[M][101];
int num[M];
int cent[5]={1,5,10,25,50};
void Get_num()
{
	memset(num,0,sizeof(num));
	memset(c1,0,sizeof(c1));
	memset(c2,0,sizeof(c2));
	for(int i=0;i<=100;i++)//初始化,每个方法数初始为1; 
		c1[i][i]=1;
	for(int i=1;i<5;i++)
	{
		for(int j=0;j<M;j++)
			for(int k=0,u=0;k+j<M;k+=cent[i],u++)
				for(int p=0;p+u<=100;p++)//限制件数 
						c2[k+j][p+u]+=c1[j][p];
		for(int i=0;i<M;i++)
			for(int j=0;j<=100;j++)//更新数组 
			{
				c1[i][j]=c2[i][j];
				c2[i][j]=0;
			}
	}
	for(int i=0;i<M;i++)
		for(int j=0;j<=100;j++)//将组成相同钱币的不同硬币数累加 
			num[i]+=c1[i][j];//得到结果 
}
int main()
{
	Get_num();
	int n;
	while(~scanf("%d",&n))
	{
		cout<<num[n]<<endl;
	}	
	return 0;
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值