BZOJ2118 墨墨的等式

题链:http://www.lydsy.com/JudgeOnline/problem.php?id=2118

Description

墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N、{an}、以及B的取值范围,求出有多少B可以使等式存在非负整数解。

Input

输入的第一行包含3个正整数,分别表示N、BMin、BMax分别表示数列的长度、B的下界、B的上界。输入的第二行包含N个整数,即数列{an}的值。

Output

输出一个整数,表示有多少b可以使等式存在非负整数解。

Sample Input

2 5 10

3 5

Sample Output

5

HINT

对于100%的数据,N≤12,0≤ai≤5*105,1≤BMin≤BMax≤1012

题解

假设x是一个可以被拼出的可行解,那么(x+k*a[i])必然也是可行解,那么我们把a[i]设为最小的a[0]即可,因为这样才能使得k最大。同时我们可以得到:

  1. x可以写成k*a[0]+i,(0<=i<a[0])
  2. 所以最多只有i个解,我们只需求出每个解的最小代价即可

    因此可以建图,每个i与(a[j]+i)%a[0],之间的代价为a[j],之后再求0到每个i之间的距离即可

参考代码

#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
#define inf 10000000000000
#define mod 1000000007
using namespace std;
ll read()
{
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int N=5e5+10;
const int M=5e6+10;
int cnt;
struct Edge{
int cost,to,nxt;
Edge(){};
Edge(int tc,int tt,int tn=0):cost(tc),to(tt),nxt(tn){}
bool operator < (const Edge &an) const{
return cost>an.cost;
}
}Path[M];
int a[20],head[N];
ll dis[N];
bool vis[N];
void Addedge(int u,int v,int w){
Path[cnt]=(Edge){w,v,head[u]};
head[u]=cnt++;
}
void Dijkstra()
{
priority_queue<Edge>que;
for(int i=0;i<a[0];i++) dis[i]=inf;
dis[0]=0;
que.push(Edge(0,0));
while(!que.empty()){
int cur=que.top().to;que.pop();
if(vis[cur])continue;
vis[cur]=true;
for(int i=head[cur];i;i=Path[i].nxt)
if(dis[cur]+Path[i].cost<dis[Path[i].to]){
dis[Path[i].to]=dis[cur]+Path[i].cost;
que.push(Edge(dis[Path[i].to],Path[i].to));
}
}
}
ll query(ll x)
{
ll ans=0;
for (int i=0;i<a[0];i++)
if (dis[i]<=x) ans+=(x-dis[i])/a[0]+1;
return ans;
}
void Init(){
cnt=1;
memset(head,0,sizeof(head));
}
int main(){
int top=0,n=read();
ll l=read(),r=read();
Init();
for(int i=0;i<n;i++){
int x=read();
if(x==0) continue;
a[top++]=x;
}
sort(a,a+top);
for(int i=0;i<a[0];i++){
for(int j=1;j<top;j++){
Addedge(i,(a[j]+i)%a[0],a[j]);
}
}
Dijkstra();
printf("%lld\n",query(r)-query(l-1));
return 0;
}

【BZOJ 2118】 墨墨的等式(Dijkstra)的更多相关文章

  1. 【BZOJ 2118】墨墨的等式

    http://www.lydsy.com/JudgeOnline/problem.php?id=2118 最短路就是为了找到最小的$x$满足$x=k×a_{min}+d,0≤d<a_{min}$ ...

  2. bzoj 2118 墨墨的等式 - 图论最短路建模

    墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在非负整数解. Input ...

  3. 数论+spfa算法 bzoj 2118 墨墨的等式

    2118: 墨墨的等式 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1283  Solved: 496 Description 墨墨突然对等式很感兴 ...

  4. 【BZOJ 2118】 2118: 墨墨的等式 (最短路)

    2118: 墨墨的等式 Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求 ...

  5. [图论训练]BZOJ 2118: 墨墨的等式 【最短路】

    Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在 ...

  6. bzoj 2118: 墨墨的等式

    Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+-+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在 ...

  7. bzoj 2118: 墨墨的等式 spfa

    题目: 墨墨突然对等式很感兴趣,他正在研究\(a_1x_1+a_2y_2+ ... +a_nx_n=B\)存在非负整数解的条件,他要求你编写一个程序,给定\(N,\{a_n\}\)以及\(B\)的取值 ...

  8. BZOJ2118墨墨的等式[数论 最短路建模]

    2118: 墨墨的等式 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1317  Solved: 504[Submit][Status][Discus ...

  9. 【BZOJ2118】墨墨的等式(最短路)

    [BZOJ2118]墨墨的等式(最短路) 题面 BZOJ 洛谷 题解 和跳楼机那题是一样的. 只不过走的方式从\(3\)种变成了\(n\)种而已,其他的根本没有区别了. #include<ios ...

随机推荐

  1. PHP 循环一个文件下的所有目录以及文件

    function test($dir) { //判断dir是否目录 if(is_dir($dir)) { $files = []; //列出 dir 目录中的文件和目录: $list = scandi ...

  2. mysql 时间向减写法

    select *  from  (   select  c.OrderNumber ,    c.Name as equipmentName,     a.*,    d.Starttime, d.E ...

  3. Luogu P1280 Niko的任务【线性dp】By cellur925

    Nikonikoni~~ 题目传送门 这是当时学长讲dp的第一道例题,我还上去献了个丑,然鹅学长讲的方法我似董非董(??? 我当时说的怎么设计这道题的状态,但是好像说的是二维,本题数据范围均在1000 ...

  4. iOS 上传APP到AppStore 卡在 Authenticating with the iTunes store 提示

    上传APP的时候,遇到了问题,一直卡在Authenticating with the iTunes store提示这里, 解决办法:在Application Loader里面登录需要上传APP的开发者 ...

  5. _bzoj3223 Tyvj 1729 文艺平衡树【Splay】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3223 裸的,打个标记. #include <cstdio> #include & ...

  6. 贪心 UVALive 6834 Shopping

    题目传送门 /* 题意:有n个商店排成一条直线,有一些商店有先后顺序,问从0出发走到n+1最少的步数 贪心:对于区间被覆盖的点只进行一次计算,还有那些要往回走的区间步数*2,再加上原来最少要走n+1步 ...

  7. HTML5 File API的应用

    HTML5 File API简介 HTML5File API协议族 Directories and System   文件系统和目录读取 FileWriter   写入文件 FileReader   ...

  8. dotnet cors 跨域问题

    研究了一整子的.net core框架,感觉挺好用的,可以用在实际项目中,前端avalon框架也在研究: 问题:跨域,相比原来的跨域解决方案,还是有不小的变化的,原来的.net api 只需要在WebA ...

  9. Objective -C Categories

    Objective -C Categories  The dynamic runtime dispatch mechanism employed by Objective-C lets you add ...

  10. 源代码管理SVN的使用

    SVN 全称是Subversion,集中式版本控制之王者 SVN 版本控制,需要自己搭建一个管理代码的服务器,提供开发人员,上传和下载 1.基本介绍 使用环境 要想利用SVN管理源代码,必须得有2套环 ...