Link: HDU 1284 coin exchange
Coin Exchange Problems
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 5467 accepted submission (s): 3123
Problem description has only one cent, two cent, and three cent coins in a country. There are many exchange methods to convert money N into coins. Compile a program to calculate the total number of exchange methods.
Each input row has only one positive integer N, and N is less than 32768.
Output corresponds to the number of exchange methods for each input.
Sample Input
293412553
Sample output
71883113137761
Authorsmallbeer (CRF)
Source hangdian ACM training team training competition (VII)
Recommendlcy | we have carefully selected several similar problems for you: 2159 1248 1203 1231
Idea 1: Evaluate the coefficient of the primary function
Code:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxn 32770int n, c1[maxn], c2[maxn];void Init(){ for(int i = 0; i <= maxn; i++) { c1[i] = 1; c2[i] = 0; } for(int i = 2; i <= 3; i++) { for(int j = 0; j <= maxn; j++) for(int k = 0; k+j <= maxn; k+=i) c2[j+k] += c1[j]; for(int j = 0; j <= maxn; j++) { c1[j] = c2[j]; c2[j] = 0; } }}int main(){ Init(); while(~scanf("%d", &n)) printf("%d\n", c1[n]); return 0;}
Idea 2: DP. The money in the future can be introduced by the money in the front.
Code:
#include <iostream>#include <cstdio>using namespace std;#define maxn 32770int n, dp[maxn];void Init(){ dp[0] = 1; for(int i = 1; i <= 3; i++) for(int j = i; j <= maxn; j++) dp[j] += dp[j-i];}int main(){ Init(); while(~scanf("%d", &n)) printf("%d\n", dp[n]); return 0;}