Money Systems
DESCRIPTION
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
THOUGHTS
dp?就是一个01背包问题,一个模板。
注意一点小细节:会炸int的哦。
DIFFICULTIES: pj
AC PROGRAM:
/*
ID: kongse_1
PROG: money
LANG: C++
*/
// Skq_Liao
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (register int i = (a), i##_end_ = (b); i < i##_end_; ++i)
#define ROF(i, a, b) for (register int i = (a), i##_end_ = (b); i > i##_end_; --i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
const char Fin[] = "money.in";
const char Fout[] = "money.out";
void In()
{
freopen(Fin, "r", stdin);
freopen(Fout, "w", stdout);
return ;
}
char Buf[1 << 20], *buf = Buf;
int GetInt()
{
int In = 0, Fi = 1;
while(!isdigit(*buf))
Fi = *buf++ == '-' ? -1 : 1;
while(isdigit(*buf))
In = In * 10 + *buf++ - '0';
return In * Fi;
}
const int MAXN = 10000 + 0120;
const int MAXM = 30;
int n, num;
int W[MAXM];
long long F[MAXN];
void Dp()
{
F[0] = 1;
FOR(i, 0, num)
{
FOR(j, W[i], n + 1)
F[j] = F[j - W[i]] + F[j];
}
return ;
}
int main()
{
In();
fread(Buf, 1, sizeof Buf, stdin);
num = GetInt(); n = GetInt();
FOR(i, 0, num)
W[i] = GetInt();
Dp();
printf("%lld\n", F[n]);
return 0;
}
Skq_Liao 2017/07/11 10:10 与AB213