USACO Section2.3 Money Systems 解题报告 【icedream61】
money解题报告
------------------------------------------------------------------------------------------------------------------------------------------------
【题目】
你有V种硬币,每种硬币面值都告诉你。
请问,用它们凑出面值N,总共有多少种情况?
【数据范围】
1<=V<=25
1<=N<=10000
数据保证,总情况数不超过long long。
【输入格式】
第一行给出V和N。
后面给出所有V种面值。
【输入样例】
3 10
1 2 5
【输出样例】
10
------------------------------------------------------------------------------------------------------------------------------------------------
【分析】
完全背包问题。
之前写过背包问题的总结,不懂的同学可以去看我之前的博文:“背包问题基本解法 —— 《背包九讲》笔记”。
------------------------------------------------------------------------------------------------------------------------------------------------
【总结】
开始对这题的数据范围并没有什么认识,于是枚举过了前7个点,第8个点超时。
自己调试了下,发现整数拆分竟然有如此多种情况。于是换用神奇的背包做了。
------------------------------------------------------------------------------------------------------------------------------------------------
【代码(注意,本题给出了两个版本的代码:背包算法和枚举法。)】
AC版本,即背包算法。
/*
ID: icedrea1
PROB: money
LANG: C++
*/ #include <iostream>
#include <fstream>
using namespace std; const int maxv = +;
const int maxn = +;
bool b[maxn];
int d[maxv]; int c[maxv];
long long cnt[maxn]; int main()
{
ifstream in("money.in");
ofstream out("money.out"); int V,N; in>>V>>N;
for(int i=,x;i<=V;++i) { in>>x; if(x<=N) b[x]=true; }
V=;
for(int i=;i<=N;++i)
if(b[i]) d[++V]=i; // 保证输入硬币是从小到大的 cnt[]=;
for(int i=;i<=V;++i)
for(int j=d[i];j<=N;++j) cnt[j]+=cnt[j-d[i]]; out<<cnt[N]<<endl; in.close();
out.close();
return ;
}
只过7个点的版本,即枚举法。
/*
ID: icedrea1
PROB: money
LANG: C++
*/ #include <iostream>
#include <fstream>
using namespace std; const int maxv = +;
const int maxn = +;
bool b[maxn];
int d[maxv]; int c[maxv];
long long ts; long long count(int n,int v) // 还剩n元,要决定d[v]的个数
{
//cout<<n<<' '<<v<<endl; //cin.get();
++ts; //if(ts%100000000==0) cout<<ts<<endl;
if(n==) return ;
if(v==) return ;
long long s=;
for(int i=;d[v]*i<=n;++i)
{
c[v]=i; s+=count(n-d[v]*i,v-);
}
return s;
} int main()
{
ifstream in("money.in");
ofstream out("money.out"); int V,N; in>>V>>N;
for(int i=,x;i<=V;++i) { in>>x; if(x<=N) b[x]=true; }
V=;
for(int i=;i<=N;++i)
if(b[i]) d[++V]=i; // 保证输入硬币是从小到大的 cout<<count(N,V)<<endl;
cout<<"ts="<<ts<<endl; in.close();
out.close();
return ;
}
USACO Section2.3 Money Systems 解题报告 【icedream61】的更多相关文章
- USACO Section2.1 The Castle 解题报告
castle解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- USACO Section2.1 Ordered Fractions 解题报告
frac1解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...
- USACO Section2.1 Healthy Holsteins 解题报告 【icedream61】
holstein解题报告 --------------------------------------------------------------------------------------- ...
- USACO Section2.2 Preface Numbering 解题报告 【icedream61】
preface解题报告----------------------------------------------------------------------------------------- ...
- USACO Section2.1 Hamming Codes 解题报告 【icedream61】
hamming解题报告----------------------------------------------------------------------------------------- ...
- USACO Section2.3 Controlling Companies 解题报告 【icedream61】
concom解题报告------------------------------------------------------------------------------------------ ...
- USACO Section2.3 Zero Sum 解题报告 【icedream61】
zerosum解题报告----------------------------------------------------------------------------------------- ...
- USACO Section2.3 Cow Pedigrees 解题报告 【icedream61】
nocows解题报告------------------------------------------------------------------------------------------ ...
- USACO Section2.3 Longest Prefix 解题报告 【icedream61】
prefix解题报告------------------------------------------------------------------------------------------ ...
随机推荐
- MySQL入门很简单: 2 MySQL数据类型
2. MySQL数据类型 2.1 整数类型 后面的是默认显示宽度: tinyint(4) smallint(6) mediumint(9) int(11) bigint(20) 2.2 浮点型和定点数 ...
- 1.10 从表中随机返回n条记录
同时使用内置函数的rand函数. limit 和order by: select * from emp order by rand() limit 2;
- 2018.7.30 Oracle的Blog数据库类型读取和存
package com.lanqiao.shopping.test; import java.io.BufferedInputStream; import java.io.BufferedOutput ...
- 2017.11.10 web中URL和URI的区别
URI:Uniform Resource Identifier,统一资源标识符: •URL:Uniform Resource Locator,统一资源定位符: •URN:Uniform Resourc ...
- R 语言爬虫 之 cnblog博文爬取
Cnbolg Crawl a). 加载用到的R包 ##library packages needed in this case library(proto) library(gsubfn) ## Wa ...
- 成员变量和成员函数前加static的作用?
成员变量和成员函数前加static的作用?答:它们被称为常成员变量和常成员函数,又称为类成员变量和类成员函数.分别用来反映类的状态.比如类成员变量可以用来统计类实例的数量,类成员函数负责这种统计的动作 ...
- java基础必备单词讲解 day six
development development development development 开发 development developmentenvironment environment en ...
- Hive[6] HiveQL 查询
6.1 SELECT ... FROM 语句 hive> SELECT name,salary FROM employees; --普通查询 hive>SELECT e.n ...
- Aspects– iOS的AOP面向切面编程的库
简介 一个简洁高效的用于使iOS支持AOP面向切面编程的库.它可以帮助你在不改变一个类或类实例的代码的前提下,有效更改类的行为.比iOS传统的 AOP方法,更加简单高效.支持在方法执行的前/后或替代原 ...
- MySQL中使用group_concat()函数数据被截取(有默认长度限制),谨慎!
最近在工作中遇到一个问题: 我们系统的一些逻辑处理是用存储过程实现的,但是有一天客服反馈说订单下单失败,查了下单牵扯到的产品基础资源,没有问题. 下单的存储过程中有这样两句代码: ; ; ; 执行存储 ...