The cows have not only created their own government but they have chosen to create their own money system. In their own rebellious way, they are curious about values of coinage. Traditionally, coins come in values like 1, 5, 10, 20 or 25, 50, and 100 units, sometimes with a 2 unit coin thrown in for good measure.
The cows want to know how many different ways it is possible to dispense a certain amount of money using various coin systems. For instance, using a system of {1, 2, 5, 10, ...} it is possible to create 18 units several different ways, including: 18x1, 9x2, 8x2+2x1, 3x5+2+1, and many others.
Write a program to compute how many ways to construct a given amount of money using supplied coinage. It is guaranteed that the total will fit into both a signed long long (C/C++) and Int64 (Free Pascal).
PROGRAM NAME: money
INPUT FORMAT
The number of coins in the system is V (1 <= V <= 25).
The amount money to construct is N (1 <= N <= 10,000).
Line 1: | Two integers, V and N |
Lines 2..: | V integers that represent the available coins (no particular number of integers per line) |
SAMPLE INPUT (file money.in)
3 10
1 2 5
OUTPUT FORMAT
A single line containing the total number of ways to construct N money units using V coins.SAMPLE OUTPUT (file money.out)
10
We use dynamic programming to count the number of ways to make n cents with the given coins. If we denote the value of the kth coin by c_k, then the recurrence is:
nway(n, k) = no. of ways to make n cents with the first k types of coins nway(n, k) = nway(n, k-1) + nway(n-c_k, k)
This just says the number of ways to make n cents with the first k coins is the number of ways to make n cents using the first k-1 coins (i.e., without using the kth coin) plus the number of ways to make n-c_k cents using the first k coins. For the second set of ways, we then add the kth coin to arrive at a total of n cents.
We keep track of the number of ways to total "n" cents in "nway", updating the array as we read the value of each coin.
/*
PROG: money
ID: rsc001
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MAXTOTAL 10000
long long nway[MAXTOTAL+1];
void
main(void)
{
FILE *fin, *fout;
int i, j, n, v, c;
fin = fopen("money.in", "r");
fout = fopen("money.out", "w");
assert(fin != NULL && fout != NULL);
fscanf(fin, "%d %d", &v, &n);
nway[0] = 1;
for(i=0; i<v; i++) {
fscanf(fin, "%d", &c);
for(j=c; j<=n; j++)
nway[j] += nway[j-c];
}
fprintf(fout, "%lld\n", nway[n]);
}
slower O(n^3) and O(n^4) solutions:
#include <cstdio> long long dp[30][10010]; int v,n,a[30]; int main() { freopen("money.in","r",stdin); freopen("money.out","w",stdout); scanf("%d%d",&v,&n); dp[0][0]=1; for(int i=1;i<=v;i++) scanf("%d",&a[i]); for(int i=1;i<=v;i++) { for(int j=0;j<a[i];j++) dp[i][j]=dp[i-1][j]; for(int j=a[i];j<=n;j++) for(int k=j;k>=0;k-=a[i]) dp[i][j]+=dp[i-1][k]; }
/*for(int i=1;i<=v;i++)for(int k=0;k<i;k++)for(int j=a[i];j<=n;j++)for(int t=j-a[i];t>=0;t-=a[i])dp[i][j]+=dp[k][t];long long ans=0;for(int i=1;i<=v;i++)ans+=dp[i][n];printf("%lld\n",ans);*/return 0;}printf("%lld\n",dp[v][n]);