您的位置:首页 > 其它

USACO 2.3.4 Money Systems 解题报告

2011-07-18 11:33 323 查看
Money Systems

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
题目大意很简单,就是给出钱币系统,求能拼出V的情况总数

可以用DP求解,注意到 money [ j ] [ i ] += money [ j - k * sys [ i - 1 ] ] [ i - 1 ] ;

其中money [ j ] [ i ] 表示用前i种币拼出j的情况总数,明显题目要求的是money [ n ] [ v ]

参考代码:


/*
ID:shiryuw1
PROG:money
LANG:C++
*/
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int sys [ 30 ] ;
long long money [ 10005 ] [ 30 ] = { 0 } ;
int main()
{
freopen("money.in","r",stdin);
freopen("money.out","w",stdout);
int v , n ;
cin >> v >> n ;
int i , j , k ;
for ( i = 0 ; i < v ; i ++ )
{
cin >> sys [ i ] ;
}
sort ( sys , sys + v );

for ( i = 0 ; i <= n ; i ++ )
{
if ( i % sys [ 0 ] == 0 )
money [ i ] [ 1 ] = 1;
else
money [ i ] [ 1 ] = 0 ;
}
for ( i = 2 ; i <= v ; i ++ )
{
for ( j = 0 ; j <= n ; j ++ )
{
for ( k = 0 ; j >= k * sys [ i - 1 ] ; k ++ )
{
money [ j ] [ i ] += money [ j - k * sys [ i - 1 ] ] [ i - 1 ] ;
}
}
}

printf ( "%lld\n" , money [ n ] [ v ] );
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: