Bzoj2118 墨墨的等式
Time Limit: 10 Sec Memory Limit: 259 MB
Submit: 1488 Solved: 578
Description
墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N、{an}、以及B的取值范围,求出有多少B可以使等式存在非负整数解。
Input
输入的第一行包含3个正整数,分别表示N、BMin、BMax分别表示数列的长度、B的下界、B的上界。输入的第二行包含N个整数,即数列{an}的值。
Output
输出一个整数,表示有多少b可以使等式存在非负整数解。
Sample Input
3 5
Sample Output
HINT
对于100%的数据,N≤12,0≤ai≤5*10^5,1≤BMin≤BMax≤10^12。
Source
同余类最短路
在所有读入的a[i]中,找到最小的一个a为基准,在模a的意义下计算问题
假设通过一些数可以凑出x,使得x%a==j,那么通过累加a就可以凑出所有%a==j的大数。
设dis[i]表示能凑到的模a余i的最小数,SPFA算出dis数组,再利用dis计算Bmin~Bmax内可以凑出的数的个数。
一:刚开始写了建边的版本,但是因为要连的边太多了,常数爆炸,4000+ms通过
- #include<iostream>
- #include<cstdio>
- #include<algorithm>
- #include<cstring>
- #include<queue>
- #define LL long long
- using namespace std;
- const int mxn=;
- int read(){
- int x=,f=;char ch=getchar();
- while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
- while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
- return x*f;
- }
- struct edge{
- int v,nxt;
- LL dis;
- }e[mxn*];
- int hd[mxn],mct=;
- void add_edge(int u,int v,LL dis){
- e[++mct].v=v;e[mct].nxt=hd[u];e[mct].dis=dis;hd[u]=mct;return;
- }
- int n;
- LL B1,B2;
- LL dis[mxn];
- int a[];
- bool inq[mxn];
- queue<int>q;
- void SPFA(){
- memset(dis,0x3f,sizeof dis);
- q.push();inq[]=;dis[]=;
- while(!q.empty()){
- int u=q.front();q.pop();inq[u]=;
- for(int i=hd[u];i;i=e[i].nxt){
- int v=e[i].v;
- if(dis[v]>dis[u]+e[i].dis){
- dis[v]=dis[u]+e[i].dis;
- if(!inq[v]){
- inq[v]=;
- q.push(v);
- }
- }
- }
- }
- return;
- }
- LL query(LL x){
- LL res=;
- for(int i=;i<a[];i++){
- if(dis[i]<=x)res+=(x-dis[i])/(LL)a[]+;
- }
- return res;
- }
- int main()
- {
- scanf("%d%lld%lld\n",&n,&B1,&B2);
- int i,j;
- for(i=;i<=n;i++){
- a[i]=read();
- if(!a[i]){i--;n--;}
- }
- sort(a+,a+n+);
- for(i=;i<=n;i++)
- for(j=;j<a[];j++){
- add_edge(j,(j+a[i])%a[],a[i]);
- }
- SPFA();
- // for(i=0;i<a[1];i++)printf("%d ",dis[i]);
- LL ans=query(B2)-query(B1-);
- printf("%lld\n",ans);
- return ;
- }
邻接表
二:改成了不具体建边的写法,只需要1000+ms
- #include<iostream>
- #include<cstdio>
- #include<algorithm>
- #include<cstring>
- #include<queue>
- #define LL long long
- using namespace std;
- const int mxn=;
- int read(){
- int x=,f=;char ch=getchar();
- while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
- while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
- return x*f;
- }
- int n;
- LL B1,B2;
- LL dis[mxn];
- int a[];
- bool inq[mxn];
- queue<int>q;
- void SPFA(){
- memset(dis,0x3f,sizeof dis);
- q.push();inq[]=;dis[]=;
- while(!q.empty()){
- int u=q.front();q.pop();inq[u]=;
- for(int i=;i<=n;i++){
- int v=(u+a[i])%a[];
- if(dis[v]>dis[u]+a[i]){
- dis[v]=dis[u]+a[i];
- if(!inq[v]){
- inq[v]=;
- q.push(v);
- }
- }
- }
- }
- return;
- }
- LL query(LL x){
- LL res=;
- for(int i=;i<a[];i++){
- if(dis[i]<=x)res+=(x-dis[i])/(LL)a[]+;
- }
- return res;
- }
- int main()
- {
- scanf("%d%lld%lld\n",&n,&B1,&B2);
- int i,j;
- for(i=;i<=n;i++){
- a[i]=read();
- if(!a[i]){i--;n--;}
- }
- sort(a+,a+n+);
- SPFA();
- LL ans=query(B2)-query(B1-);
- printf("%lld\n",ans);
- return ;
- }
Bzoj2118 墨墨的等式的更多相关文章
- 【BZOJ2118】墨墨的等式(最短路)
[BZOJ2118]墨墨的等式(最短路) 题面 BZOJ 洛谷 题解 和跳楼机那题是一样的. 只不过走的方式从\(3\)种变成了\(n\)种而已,其他的根本没有区别了. #include<ios ...
- 【BZOJ2118】墨墨的等式 最短路
[BZOJ2118]墨墨的等式 Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值 ...
- BZOJ2118墨墨的等式[数论 最短路建模]
2118: 墨墨的等式 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1317 Solved: 504[Submit][Status][Discus ...
- BZOJ2118: 墨墨的等式(同余类BFS)(数学转为图论题)
2118: 墨墨的等式 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2944 Solved: 1206[Submit][Status][Discu ...
- BZOJ2118:墨墨的等式(最短路)
Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在 ...
- p2371&bzoj2118 墨墨的等式
传送门(bzoj) 题目 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存 ...
- BZOJ2118: 墨墨的等式(最短路 数论)
题意 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在非负整数解. So ...
- BZOJ2118: 墨墨的等式(最短路构造/同余最短路)
Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在 ...
- 【BZOJ 2118】 墨墨的等式(Dijkstra)
BZOJ2118 墨墨的等式 题链:http://www.lydsy.com/JudgeOnline/problem.php?id=2118 Description 墨墨突然对等式很感兴趣,他正在研究 ...
随机推荐
- 如何在mac本上安装android sdk
众所周知的原因,google的很多网站在国内无法访问,苦逼了一堆天朝程序员,下是在mac本上折腾android 开发环境的过程: 一.先下载android sdk for mac 给二个靠谱的网址: ...
- WP7开发 Sqlite数据库的使用 解决Unable open the database
WP7本身不支持Sqlite数据库,但我们可以添加第三方组件让它支持Sqlite. 首先在项目中添加引用Community.CsharpSqlite.WP.dll,我会放后面让大家下载,我下了有几天了 ...
- 图片加载框架Picasso解析
picasso是Square公司开源的一个Android图形缓存库 主要有以下一些特性: 在adapter中回收和取消当前的下载: 使用最少的内存完成复杂的图形转换操作: 自动的内存和硬盘缓存: 图形 ...
- CoreBluetooth——IOS蓝牙4.0使用心得
原文链接:http://m.blog.csdn.net/article/details?plg_nld=1&id=51014318&plg_auth=1&plg_uin=1&a ...
- 在线音乐网站【03】Part one 功能实现
今天打算把网站功能的具体实现给总结一下,如果你想了解整个小项目,建议你先看看前面2篇博客. 1.在线音乐网站(1)需求和功能结构 2.在线音乐网站(2)数据库和开发环境 7.网站主要模块实现 a.在线 ...
- Theano2.1.16-基础知识之调试:常见的问题解答
来自:http://deeplearning.net/software/theano/tutorial/shape_info.html Debugging Theano: FAQ and Troubl ...
- unity3d 三分钟实现简单的赛车漂移
提到赛车游戏,大家最关心的应该就是漂移吧?! 从学unity开始,我就一直在断断续续的研究赛车 因为自己技术太烂.悟性太差等原因,我走了不少弯路 也许你会说,网上那么多资料,你不会查啊 是啊!网上一搜 ...
- MySQL5.6 实现主从复制,读写分离,分散单台服务器压力
闲来无事,在本地搭建几台虚拟机,准备配一个mysql读写分离的主从配置,版本选用最新版的,mysql.5.6.28 版本,本处使用源码安装(鄙人一向喜欢源码安装,因为centos中鄙人不知道yum安装 ...
- Angular实现瀑布流的库angular-deckgrid
一. 安装 bower install --save angular-deckgrid 添加代码到你的HTML 添加到你的angular模块中: angular.module('your.module ...
- 使用Aspose.Cells生成Excel的方法详解(转)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System ...