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 墨墨突然对等式很感兴趣,他正在研究 ...
随机推荐
- CentOS上搭建Nginx + Mono 运行 asp.net
安装步骤: 一.获取开源相关程序: 1.利用CentOS Linux系统自带的yum命令安装.升级所需的程序库: sudo -sLANG=Cyum -y install gcc gcc-c++ aut ...
- rpc框架: thrift/avro/protobuf 之maven插件生成java类
thrift.avro.probobuf 这几个rpc框架的基本思想都差不多,先定义IDL文件,然后由各自的编译器(或maven插件)生成目标语言的源代码,但是,根据idl生成源代码这件事,如果每次都 ...
- CentOS7 下安装JDK1.7 和 Tomcat7
一.下载JDK 和 Tomcat安装包 1.JDK1.7 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downl ...
- Java面试知识点总结
本篇文章会对面试中常遇到的Java技术点进行全面深入的总结,帮助我们在面试中更加得心应手,不参加面试的同学也能够借此机会梳理一下自己的知识体系,进行查漏补缺(阅读本文需要有一定的Java基础:若您初涉 ...
- 解决"415 Cannot process the message because the content type 'application/x-www-form-urlencoded' was not the expected type 'text/xml; charset=utf-8'"
wcf basicHttpBinding content-type text/xml;charset=utf-8 wsHttpBinding 'application/soap+xml; ch ...
- canvas三角函数做椭圆运动效果
<canvas id="canvas" width="800" height="400" style="background ...
- 安卓activity生命周期
相信不少朋友也已经看过这个流程图了,也基本了解了Activity生命周期的几个过程,我们就来说一说这几个过程. 1.启动Activity:系统会先调用onCreate方法,然后调用onStart方法, ...
- oracle主键自增
oracle主键自增 1建立数据表 create table Test_Increase( userid number(10) primary key, /*主键,自动增加*/ ...
- bzoj3224
学习了下treap #include<iostream> #include<cstdio> #include<cstdlib> using namespace st ...
- Spring Security3学习实例
Spring Security是什么? Spring Security,这是一种基于Spring AOP和Servlet过滤器的安全框架.它提供全面的安全性解决方案,同时在Web请求级和方法调用级处理 ...