Coin Change
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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
4 13
————————————————————困惑の分割線————————————————————
前言:01背包双约束求方案数的一道DP题想用母函数做,但是卡住了,所以找了一个完全背包试试……
思路:母函数,套模板~注意增加一个维度表示个数的限制。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int P = 250, M = 100;
int c1[P+1][M+1], c2[P+1][M+1], v[] = {1, 5, 10, 25, 50};
int main()
{
#ifdef J_Sure
// freopen("000.in", "r", stdin);
// freopen(".out", "w", stdout);
#endif
memset(c1, 0, sizeof(c1));
c1[0][0] = 1;
for(int i = 0; i < 5; i++) {
memset(c2, 0, sizeof(c2));
for(int j = 0; j*v[i] <= P; j++) {
for(int k = 0; k + j*v[i] <= P; k++) {
for(int l = 0; l + j <= M; l++) {
c2[k+j*v[i]][j+l] += c1[k][l];
}
}
}
memcpy(c1, c2, sizeof(c2));
}
int n;
while(~scanf("%d", &n)) {
int ans = 0;
for(int i = 0; i <= M; i++) ans += c1[n][i];
printf("%d\n", ans);
}
return 0;
}