首页 > ACM题库 > HDU-杭电 > hdu 2069 Coin Change-母函数-[解题报告]C++
2013
12-29

hdu 2069 Coin Change-母函数-[解题报告]C++

Coin Change

问题描述 :

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.

输入:

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

输出:

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

样例输入:

11
26

样例输出:

4
13

// Time 0ms, Memory 404K
#include<stdio.h>
int a[255][105],temp[255][105];
int val[6]={0,1,5,10,25,50};
int num[255]={0};
int main()
{
	int i,j,k,t,n;
	a[0][0]=1;
	for(i=1;i<=5;i++)
	{
		for(j=0;j<=250;j++)
			for(k=0;j+k*val[i]<=250;k++)
				for(t=0;t+k<=100;t++)
					temp[j+k*val[i]][t+k]+=a[j][t]; 
		for(j=0;j<=250;j++)
			for(t=0;t<=100;t++)
			{
				a[j][t]=temp[j][t];
				temp[j][t]=0;
			}
	}
	for(i=0;i<=250;i++)
		for(j=0;j<=100;j++)
			num[i]+=a[i][j];
	while(scanf("%d",&n)!=EOF)
	{
		printf("%d\n",num[n]);
	}
	return 0;
}

解题转自:http://blog.csdn.net/qhc_20130731/article/details/9050955


  1. 为什么for循环找到的i一定是素数叻,而且约数定理说的是n=p1^a1*p2^a2*p3^a3*…*pk^ak,而你每次取余都用的是原来的m,也就是n