BZOJ1708: [Usaco2007 Oct]Money奶牛的硬币

1708: [Usaco2007 Oct]Money奶牛的硬币

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 513  Solved: 329
[Submit][Status]

Description

在创立了她们自己的政权之后,奶牛们决定推广新的货币系统。在强烈的叛逆心理的驱使下,她们准备使用奇怪的面值。在传统的货币系统中,硬币的面值通常是1,5,10,20或25,50,以及100单位的货币,有时为了更方便地交易,会发行面值为2单位的硬币。 奶牛们想知道,对于一个给定的货币系统,如果需要正好凑出一定数量的钱,会有多少种不同的方法。比如说,你手上有无限多个面值为{1,2,5,10,...}的硬币,并且打算凑出18单位货币,那么你有多种方法来达到你的目的:18*1,9*2,8*2+2*1,3*5+2+1,以及其他的未列出的若干方案。 请你写一个程序,帮奶牛们计算一下,如果想用有V (1 <= V <= 25)种面值的硬币,凑出总价值为N(1 <= N <= 10,000)的一堆钱,一共有多少种不同的方法。答案保证不会超出C/C++中的'long long',Pascal中的'Int64',或是Java中的'long'的范围。

Input

* 第1行: 2个用空格隔开的整数:V和N

* 第2..V+1行: 每行1个整数,表示1种硬币面值

Output

* 第1行: 输出1个正整数,表示用这V种面值的硬币,凑出N单位的货币的不同方法总数。

Sample Input

3 10
1
2
5

Sample Output

10

HINT

 

Source

题解:
无限背包。。。竟然是金组的题。。。
代码:
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 10000+100
14 #define maxm 500+100
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 using namespace std;
19 inline int read()
20 {
21     int x=0,f=1;char ch=getchar();
22     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
23     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
24     return x*f;
25 }
26 int n,m;
27 ll f[maxn];
28 int main()
29 {
30     freopen("input.txt","r",stdin);
31     freopen("output.txt","w",stdout);
32     n=read();m=read();
33     f[0]=1;
34     for(int i=1;i<=n;i++)
35     {
36         int x=read();
37         for(int j=x;j<=m;j++)f[j]+=f[j-x];
38     } 
39     printf("%lld
",f[m]);
40     return 0;
41 }
View Code
原文地址:https://www.cnblogs.com/zyfzyf/p/3941367.html