(简单母函数进阶版,暴力)hdu 2069 Coin Change

Coin Change

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 14857    Accepted Submission(s): 5024

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

浙江工业大学网络选拔赛

考察知识点:二维母函数。

//考查知识点:简单母函数进阶版----二维母函数 表示苦涩难懂。。。
/**********************************************************************************************
二维母函数的模板大致如下:
int c1[maxn][maxe],c2[maxn][mare];
int a[num];
第一步:简单初始化
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
第二步:使计算有循环区间,就是有值可以相加,相加零毕竟无意义。
for(i=0;i<=maxe;++i)//inf为其中的限制条件
c1[i]=1;
第三步:对整个表达式进行逐个两两计算。
for(i=2;i<=num-1;++i)//n-1个表达式,因为第一个与第二个合成的结果记为新的第一个表达式
{
	for(j=0;j<=n;++j)//对所求的n个表达式之内的值进行计算,j表示前面j个表达式的结果中的第j项
	{
		for(k=0;k+j<=n;k+=a[i])
		{
			for(l=0;l+k/a[i]<=maxe;l++)
			c2[j+k][l+k/a[i]]+=c1[j][l];
		}
	}
	for(j=0;j<=n;++j)
	{
		for(k=0;k<=maxe;++k)
		{
			c1[j][k]=c[j][k];
			c2[j][k]=0;
		}
	}
}
第四步:最后n的组成情况为:
for(i=0;i<=maxe;++i)
sum+=c1[n][i];
printf("%d\n",sum);
使用情况:可以组分的情况有多种,另外设立一个数组,主要的变化是在k的循环进行。
**********************************************************************************/
#include<stdio.h>
#include<string.h>
int c1[252][102],c2[252][102];
int a[6]={0,1,5,10,25,50};
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		if(n==0)
		{
			printf("1\n");
			continue;
		}
		int sum=0;
		memset(c1,0,sizeof(c1));
		memset(c2,0,sizeof(c2));
		int i,j,k,l;
		for(i=0;i<=100;++i)
		{
			c1[i][i]=1;////////////////////////
		}
		for(i=2;i<=5;++i)
		{
			for(j=0;j<=n;++j)
			{
				for(k=0;k+j<=n;k+=a[i])
				{
					for(l=0;l+k/a[i]<=100;l++)
					c2[j+k][l+k/a[i]]+=c1[j][l];///////////////
				}
			}
			for(j=0;j<=n;++j)
			{
				for(k=0;k<=100;++k)
				{
					c1[j][k]=c2[j][k];
					c2[j][k]=0;
				}
			}
		}
		for(i=0;i<=100;++i)
		sum+=c1[n][i];
		printf("%d\n",sum);
	}
	return 0;
}

法二:当初做的时候没想到-_-||,简单的暴力竟然也能过。。。

//法二:竟然忘了一个最水的办法:直接暴力-_-||
#include<stdio.h>
int main()
{
	int i,j,k,l;
	int n;
	while(~scanf("%d",&n))
	{
		if(!n)
		{
			printf("1\n");
			continue;
		}
		int sum=0;
		for(i=n/50;i>=0;i--)
		{
			for(j=(n-i*50)/25;j>=0;j--)
			{
				for(k=(n-i*50-j*25)/10;k>=0;k--)
				{
					for(l=(n-i*50-j*25-k*10)/5;l>=0;l--)
					{
						if(i+j+k+l+n-i*50-j*25-k*10-l*5<=100)
						sum++;
					}
				}
			}
		}
		printf("%d\n",sum);
	}
	return 0;
} 

时间: 2024-10-20 16:28:02

(简单母函数进阶版,暴力)hdu 2069 Coin Change的相关文章

HDU 2069 Coin Change 母函数求解

HDU 2069 Coin Change 母函数求解 题目:http://acm.hdu.edu.cn/showproblem.php?pid=2069 这题比普通的母函数求解题目复杂一点,多了组成个数的限制, 要求组合个数不能超过 100 个硬币,所以将 c1, c2 定义为二维数组, c1[i][j] 表示 c1[结果][组成该结果的个数] ,然后多了一层遍历个数的循环. // 母函数求解 #include <bits/stdc++.h> using namespace std; cons

UVa 674 &amp; hdu 2069 Coin Change (母函数,dp)

链接:uva 674 题意:有5中货币,价值分别为 50-cent, 25-cent, 10-cent, 5-cent,1-cent,数量都为无限个, 给定一个数 n,求用上述货币组成价值为 n 的方法有多少? 分析:因为n<=7489,可以用 母函数 或 dp 打表 对于dp状态方程为: dp[j]+=dp[j-c[i]] 母函数: #include<stdio.h> int c1[7500],c2[7500],w[5]={1,5,10,25,50};; void mhs() { in

hdu 2069 Coin Change 背包。本来打算用母函数再写一遍的,发现代码极其相似,就没写

Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14982    Accepted Submission(s): 5070 Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and

HDU 2069 Coin Change

Coin Change Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 206964-bit integer IO format: %I64d      Java class name: Main Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We

hdu 2069 Coin Change(完全背包)

Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16592 Accepted Submission(s): 5656 Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent.

HDU 2069 Coin Change (经典DP)

Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14500    Accepted Submission(s): 4879 Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1

hdu 2069 Coin Change (dp )

dp #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<iostream> using namespace std; int op[10]={1,5,10,25,50}; int dp[300][300]; void fun() { int i,j,k; memset(dp,0,sizeof(dp)); dp[0][0]=1; for(j

HDU 2069二维母函数

显然母函数,有一个地方需要注意.当输入0时,只有一种方法,所以输出1. 代码: 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #include <iostream> 5 using namespace std; 6 int c1[255][111], c2[255][111]; 7 main() 8 { 9 10 int n, i, j, k, l; 11 int a[

hdu 2069 限制个数的母函数

Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17266    Accepted Submission(s): 5907 Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and