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(1 ~ 25),N(1 ~ 10000),代表有 V 种货币,N 的钱数,后给出这 V 种货币,每种货币的提供量无限。输出有多少凑钱数的方法刚好达到 N 这么多钱。
思路:
DP。完全背包。
设 dp[ i ][ j ] 代表当选择第 i 种钱币时,凑足 j 元钱的方法数。同0,1背包的思路相似,每种钱币都有两种选择情况,选或者不选。
选:设 k 代表可以选择 k 张 i 的钱币,所以 dp [ i ][ j ] += dp [ i - 1 ][ j - k * mon[ i ] ] ;
不选:dp [ i ][ j ] += dp [ i - 1 ][ j ] ;
循环 K 的时候从0开始即可以两种都一起考虑到了,因为 0 即代表不选。 注意方法数用 long long 即可。
AC:
/* TASK:money LANG:C++ ID:sum-g1 */ #include<stdio.h> #include<string.h> long long dp[30][10005]; int mon[30]; int main() { freopen("money.in","r",stdin); freopen("money.out","w",stdout); int v,n; scanf("%d%d",&v,&n); memset(dp,0,sizeof(dp)); for(int i = 0;i <= v;i++) dp[i][0] = 1; for(int i = 1;i <= v;i++) scanf("%d",&mon[i]); for(int i = 1;i <= v;i++) { for(int j = 1;j <= n;j++) { for(int k = 0;k * mon[i] <= j;k++) dp[i][j] += dp[i - 1][j - k * mon[i]]; } } printf("%lld\n",dp[v][n]); }
相关推荐
Money游戏Money游戏Money游戏
" -Dee Even, Senior Investment Officer, Allstate Insurance Company, Property & Casualty "The Psychology of Money represents a major step toward development of a portfolio theory that recognizes human ...
Money&You是风靡全球三十多年的美国课程。套用内地一个说法,这个课程一手谈“mon e y ” ,一手谈“you”,主张“两手都要硬”。 每位Money&You毕业生在上完课程时的一个感观都是:“我早就应该来上Money&You,如果我...
MONEY BOX WINDOWS FORMS IN C SHARP
Money exchanger programm with delphi
each using specific forms of money. The business-to-consumer (B2C) payment is used in commercial activities where the merchant is paid directly by the consumer for goods or services. This type of ...
jackson-datatype-money, 扩展模块以正确支持 javax.money的数据类型 的数据类型货币 Jackson数据类型 Money Jackson是一个支持JSON序列化和反序列化 JavaMoney用户定义的数据类型的Jackson 。 它充满了一个位置,它...
Honey money屏保 Honey money屏保 Honey money屏保 Honey money屏保
Guess the money that is available in the user packet
It is a banking project developed in C++. It contains banking transactions like create account, deposit money, withdraw money etc.
documentation about how can I earn money form nowereh
automated tellermachine program for money withdrawing
Laravel开发-money 我的第一个包,转换和管理数字的钱
Extreme.Money.pdf - financial books
Money Pile -EA基于多时间框架分析公式,并基于用于交易货币的算法数学公式,它与外汇专业人士和大型机构使用的方法相同,该系统根据市场规定,趋势和更正进行设计,Money Pile -EA能够在范围内和通过大动作赚取利润...
Laravel开发-laravel-money 带有立面的简单类,用于处理Laravel 5.2的资金
sql数据库,aaa,设定一个表,user,三个字段,username,password,money 从数据库备份,还原,删除的语句到select的语句
实现Money类的“+”、“-”、“==”以及“” 、“ >>”的运算符重载,并设计测试程序
Jackson-datatype-money - 开源的Jackson模块,支持Java货币数据类型的JSON序列化和反序列化