hdoj 2069 Coin Change 【母函数 不错的题】【限制 组合单位的个数】

Coin Change

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


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
 

题目要求:用不超过100个硬币(从1,5,10,25,505种硬币选择) 组合成 n 的组合数。

 

限制 组合 单位个数 <= 100,需在母函数模版上再添上2层循环 来进行限制和更新。

 

母函数:0ms

#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 250+10
using namespace std;
int c1[MAX][101];//c1[i][j] 表示 用j个硬币能组合成i的 组合数 
int c2[MAX][101];//媒介 
int num[MAX];//num[i]表示 用不超过100个硬币能组合成i  的 组合数 
int coin[6] = {0, 1, 5, 10, 25, 50};
void getnum()
{
	int k, i, j, p;
	int use;//当前硬币已经使用的个数 
	memset(c1, 0, sizeof(c1));
	memset(c2, 0, sizeof(c2));
	memset(num, 0, sizeof(num));
	for(i = 0; i <= 100; i++)
	c1[i][i] = 1;
	for(i = 2; i <= 5; i++)
	{
		for(j = 0; j < MAX; j++)//最终项数 
		{
			for(k = 0, use = 0; k+j < MAX; k += coin[i], use++)//use记录当前硬币使用个数 
			{
				for(p = 0; p+use <= 100; p++)//控制硬币个数 不能超过100 
				{
					c2[k+j][p+use] += c1[j][p];
				}
			}
		}
		for(j = 0; j < MAX; j++)//更新 
		{
			for(p = 0; p <= 100; p++)
			{
				c1[j][p] = c2[j][p];
				c2[j][p] = 0;
			}
		}
	} 
	for(i = 0; i < MAX; i++)
	{
		for(j = 0; j <= 100; j++)//累加100限制下的所有 组合数 
		{
			num[i] += c1[i][j];
		}
	}
}
int main()
{
	int n;
	getnum();//打表 
	while(scanf("%d", &n) != EOF)
	{
		printf("%d\n", num[n]);
	}
	return 0;
} 


 

背包:0ms


 

#include <cstdio>
#include <cstring>
using namespace std;
int dp[251][101];//dp[i][j]记录 用j个硬币 组合成i 的组合数 
int num[251]; 
int coin[6] = {0, 1, 5, 10, 25, 50};
void getnum()
{
	int k, i, j, p;
	memset(dp, 0, sizeof(dp));
	memset(num, 0, sizeof(num)); 
	dp[0][0] = 1;
	for(i = 1; i <= 5; i++)
	{
		for(j = 0; j <= 250; j++)
		{
			for(k = 0; k < 100; k++)//这里k不能等于 100 
			{
				if(coin[i]+j <= 250)//还能加 就加 
				dp[coin[i]+j][k+1] += dp[j][k];//更新 注意硬币数自增一 
				else//过了250 不用考虑 
				break;
			}
		}
	}
	for(i = 0; i <= 250; i++)
	{
		for(j = 0; j <= 100; j++)
		num[i] += dp[i][j];
	}
}
int main()
{
	int n;
	getnum();
	while(scanf("%d", &n) != EOF)
	{
		printf("%d\n", num[n]);
	}
	return 0;
} 


 

你可能感兴趣的:(hdoj 2069 Coin Change 【母函数 不错的题】【限制 组合单位的个数】)