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解题报告------------------------------------------------------------------------------------------ ...
随机推荐
- IOS 多线程-pthread
#import <pthread.h> @interface HMViewController () - (IBAction)btnClick; @end @implementation ...
- Last_SQL_Errno: 1050
主库上create table,从库上存在. 报错信息如下所示: Last_SQL_Errno: 1050 Last_SQL_Error: ...
- Compass Card Sales(模拟)
Compass Card Sales 时间限制: 3 Sec 内存限制: 128 MB提交: 35 解决: 13[提交] [状态] [讨论版] [命题人:admin] 题目描述 Katla has ...
- pyinstaller打包后的exe退出时,类中的__del__不执行问题
关于pyinstaller打包后的exe退出时,类中的__del__不执行问题,完善中
- 缓冲区溢出实战教程系列(二):dev c++编译汇编代码
小伙伴们对我上一篇文章的反应完全出乎了我的意料,感谢大家对我的支持和认可.接下来我会精心的把这一系列课程设计好,尽量详细的展示给大家.上篇文章我列举了一个缓冲区溢出的小例子,并提到了dev c++.o ...
- Android笔记(预安装APK)
一般一个安卓的产品在出厂时,会预安装许多APK,关于这些APP,主要分为下面这几类 1.系统级别APK 这一类应用一般是:电话/设置或者厂家自己特定的应用. 2.系统预安装APK 因为商业原因,产品出 ...
- 旧文备份:rtlinux安装手册
前段时间接触了几天RTLinux,折腾了好几天才总算把它安装上,得益于Prof. Chang-Gun Lee的安装建议,觉得该文档可能会对准备尝试安装RTLinux的朋友们有帮助,本人英语很烂,也比较 ...
- override与重载的区别
override与重载的区别override 与重载的区别,重载是方法的名称相同.参数或参数类型不同,进行多次重载以适应不同的需要 Override 是进行基类中函数的重写.
- 时间戳与QDateTime相互转换
最近项目中需要将日期时间输出到Excel中,程序使用Qt开发,使用第三方库QtXlsx进行Excel读写操作.Excel中第一列为时间,时间间隔为1小时,如图所示. 赋值起始时间stDTime,则后续 ...
- JS - 把类似document.querySelectorAll(".xxx")、document.getElementsByName("xxx")这种方法的返回结果转换成数组对象
var btns = document.querySelectorAll(".btn");console.log(btns instanceof Array); // falseb ...