Money Systems solution to a problem Luo Gu P1474 monetary system

P1474 monetary system Money Systems

Title Description

Cows will not only create their own government and chose to set up their own monetary system. Due to their special way of thinking, they are curious about value for money.

Conventionally, a system is composed of units of currency denominations 1,5,10,20 or 25, 50, and 100 thereof.

Cows like to know how many different ways to construct a value determined by monetary systems currency.

For example, use of a monetary system {1,2,5,10, ...} generating unit 18 denomination some possible methods are: 18x1, 9x2, 8x2 + 2x1, 3x5 + 2 + 1, like the other. Write a program to calculate how many ways a given currency system to construct a number of face value. Will ensure that the total number of suitable long long (C / C ++) and Int64 (Free Pascal), i.e., between 0 and 2 ^ 63-1.

Input Format

The number of kinds of money is money system V (1 <= V <= 25). To construct the number of money is N (1 <= N <= 10,000).

First line: two integer, V and N.

The second line: the face value of the currency available.

Output formats:

Output Format

A single line contains the number n of possible gather monetary unit with this kind v Coin Program.

Sample input and output

Input # 1

3 10
1 2 5

Output # 1

10

Description / Tips

Translation from NOCOW

USACO 2.3

[Thinking]

Full backpack
is water a backpack full title

[Title] effect

Some currency denominations
combined into n worth a total of how many kinds of combinations

[Core] ideas

If you have a, b, c face value of the coins
you need to gather acioi denomination
so simple to see acioi is
the number of combinations acioi is not that
(acioi - a), (acioi - b) and (acioi - c) three denominations and composition of ways?
So this can run completely backpack
just add here is
not the most value

[Complete code]

#include<iostream>
#include<cstdio>
#define int long long 

using namespace std;
const int Max = 10005;
int b[Max] = {1},a[30];//默认b[0]为1,也就是在代码中减去一种硬币的面额之后等于0,是这一种硬币一个就可以组成的,所以次数是1 
signed main()
{
    int v,n;
    cin >> v >> n;
    for(register int i = 1;i <= v;++ i)
        cin >> a[i];
    for(register int i = 1;i <= v;++ i)
        for(register int j = a[i];j <= n;++ j)
            b[j] += b[j - a[i]];
    cout << b[n] << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/acioi/p/11703765.html