Coin Change
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 20457 Accepted Submission(s): 7201
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.
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
413
题意:有五种面值的硬币,50,25,10,5,1。现在给你一个价值a,a<=250,问a可以由多少种总硬币数不超过100的组合方法搭配而成。
思路:一般的母函数是控制各个因子的系数范围的,而这道题是控制系数和的范围,我们可以开二维数组,在第二维存使用的硬币个数,然后在循环的时候也加一层对硬币个数的循环就行了,最后求答案的时候要把第二维从0加到100。具体看代码。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
#include <functional>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1e9 + 5;
const int MAXN = 307;
const int MOD = 1e9+7;
const double eps = 1e-8;
const double PI = acos(-1.0);
LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b);}
int Num;//因子个数
int n1[MAXN];//n1[i]表示该乘积表达式第i个因子的起始系数
int n2[MAXN];//n2[i]表示该乘积表达式第i个因子的终止系数
int v[MAXN];//v[i]表示该乘积表达式第i个因子的权重
int P;//P是可能的最大指数
int a[MAXN][105], b[MAXN][105];//a为计算结果,b为中间结果。第二维存硬币个数
int last;//最大的指数位置
void solve()
{
memset(a, 0, sizeof(a));
a[0][0] = 1;
for (int i = 1; i <= Num; i++)//循环每个因子
{
memset(b, 0, sizeof(b));
for (int j = n1[i]; j <= n2[i] && j*v[i] <= P; j++)//循环每个因子的每一项
for (int k = 0; k + j*v[i] <= P; k++)//循环a的每个项
for (int l = 0; l+j <= 100; l++)//控制硬币总数
b[k + j*v[i]][l+j] += a[k][l];//把结果加到对应位
memcpy(a, b, sizeof(b));//b赋值给a
}
}
void init()
{
Num = 5;
v[1] = 1;
v[2] = 5;
v[3] = 10;
v[4] = 25;
v[5] = 50;
n1[1] = 0;
n1[2] = 0;
n1[3] = 0;
n1[4] = 0;
n1[5] = 0;
n2[1] = 250;
n2[2] = 50;
n2[3] = 25;
n2[4] = 10;
n2[5] = 5;
P = 250;
}
int main()
{
int x;
int ans;
init();
solve();
while (scanf("%d", &x) != EOF)
{
ans = 0;
for (int i = 0; i <= 100; i++)
ans += a[x][i];
printf("%d\n", ans);
}
}