usaco2.3.4 Money Systems

本文介绍了一种使用完全背包算法解决货币系统中不同组合数量的问题。通过两段AC代码示例,展示了如何计算给定金额下使用不同面额硬币的所有可能组合数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 原题

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



二 分析

完全背包。


三 代码

运行结果1:
USER: Qi Shen [maxkibb3]
TASK: money
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 6524 KB]
   Test 2: TEST OK [0.000 secs, 6524 KB]
   Test 3: TEST OK [0.000 secs, 6524 KB]
   Test 4: TEST OK [0.000 secs, 6524 KB]
   Test 5: TEST OK [0.011 secs, 6524 KB]
   Test 6: TEST OK [0.011 secs, 6524 KB]
   Test 7: TEST OK [0.000 secs, 6524 KB]
   Test 8: TEST OK [0.000 secs, 6524 KB]
   Test 9: TEST OK [0.000 secs, 6524 KB]
   Test 10: TEST OK [0.086 secs, 6524 KB]
   Test 11: TEST OK [0.000 secs, 6524 KB]
   Test 12: TEST OK [0.335 secs, 6524 KB]
   Test 13: TEST OK [0.000 secs, 6524 KB]

All tests OK.

YOUR PROGRAM ('money') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.

AC代码1:
/*
ID:maxkibb3
LANG:C++
PROG:money
*/

#include<cstdio>

const int MAX_V = 30;
const int MAX_N = 10005;
int v, n;
int a[MAX_V];
long long dp[MAX_V][MAX_N];

int main() {
    freopen("money.in", "r", stdin);
    freopen("money.out", "w", stdout);
    scanf("%d%d", &v, &n);
    for(int i = 1; i <= v; i++) scanf("%d", &a[i]);
    dp[0][0] = 1;
    for(int i = 1; i <= v; i++) {
        for(int j = 0; j <= n; j++) {
            for(int k = 0; k <= j / a[i]; k++) {
                dp[i][j] += dp[i - 1][j - k * a[i]];
            }
        }
    }
    printf("%lld\n", dp[v][n]);
    return 0;
}

运行结果2:
USER: Qi Shen [maxkibb3]
TASK: money
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 6524 KB]
   Test 2: TEST OK [0.000 secs, 6524 KB]
   Test 3: TEST OK [0.000 secs, 6524 KB]
   Test 4: TEST OK [0.000 secs, 6524 KB]
   Test 5: TEST OK [0.000 secs, 6524 KB]
   Test 6: TEST OK [0.000 secs, 6524 KB]
   Test 7: TEST OK [0.000 secs, 6524 KB]
   Test 8: TEST OK [0.000 secs, 6524 KB]
   Test 9: TEST OK [0.000 secs, 6524 KB]
   Test 10: TEST OK [0.000 secs, 6524 KB]
   Test 11: TEST OK [0.000 secs, 6524 KB]
   Test 12: TEST OK [0.000 secs, 6524 KB]
   Test 13: TEST OK [0.000 secs, 6524 KB]

All tests OK.

Your program ('money') produced all correct answers! This is your submission #2 for this problem. Congratulations!

AC代码2:
/*
ID:maxkibb3
LANG:C++
PROG:money
*/

#include<cstdio>

const int MAX_V = 30;
const int MAX_N = 10005;
int v, n;
int a[MAX_V];
long long dp[MAX_V][MAX_N];

int main() {
    freopen("money.in", "r", stdin);
    freopen("money.out", "w", stdout);
    scanf("%d%d", &v, &n);
    for(int i = 1; i <= v; i++) scanf("%d", &a[i]);
    dp[0][0] = 1;
    for(int i = 1; i <= v; i++) {
        for(int j = 0; j <= n; j++) {
            dp[i][j] = dp[i - 1][j];
            if(j >= a[i]) dp[i][j] += dp[i][j - a[i]];
        }
    }
    printf("%lld\n", dp[v][n]);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值