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

2 5 10
3 5

Sample Output

5

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 墨墨的等式的更多相关文章

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

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

  2. 【BZOJ2118】墨墨的等式 最短路

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

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

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

  4. BZOJ2118: 墨墨的等式(同余类BFS)(数学转为图论题)

    2118: 墨墨的等式 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 2944  Solved: 1206[Submit][Status][Discu ...

  5. BZOJ2118:墨墨的等式(最短路)

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

  6. p2371&bzoj2118 墨墨的等式

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

  7. BZOJ2118: 墨墨的等式(最短路 数论)

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

  8. BZOJ2118: 墨墨的等式(最短路构造/同余最短路)

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

  9. 【BZOJ 2118】 墨墨的等式(Dijkstra)

    BZOJ2118 墨墨的等式 题链:http://www.lydsy.com/JudgeOnline/problem.php?id=2118 Description 墨墨突然对等式很感兴趣,他正在研究 ...

随机推荐

  1. 通用权限管理系统多语言开发接口 - java,php 调用接口程序,多业务子系统集成

    1:公司里有多个业务系统,需要进行统一重构,有PHP的.有Java的.有.NET的,甚至还有delphi的. 2:公司里有多个数据库系统,有mysql的.有sqlserver的.还有oracel的,甚 ...

  2. Kafka是分布式发布-订阅消息系统

    Kafka是分布式发布-订阅消息系统 https://www.biaodianfu.com/kafka.html Kafka是分布式发布-订阅消息系统.它最初由LinkedIn公司开发,之后成为Apa ...

  3. node基础06:回调函数

    1.Node异步编程 Node.js 异步编程的直接体现就是回调. 异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了. 回调函数在完成任务后就会被调用,Node 使用了大量的回调函数,No ...

  4. Oracle 创建 split 和 splitstr 函数

    Sql语句最好依次执行创建 /************************************** * name:        split * author:      sean zhang ...

  5. [MetaHook] SearchPattern function

    By Nagi void *SearchPattern(void *pStartSearch, DWORD dwSearchLen, char *pPattern, DWORD dwPatternLe ...

  6. 在线文档预览方案-office web apps

    最近在做项目时,要在手机端实现在线文档预览的功能.于是百度了一下实现方案,大致是将文档转换成pdf,然后在通过插件实现预览.这些方案没有具体实现代码,也没有在线预览的地址,再加上项目时间紧迫.只能考虑 ...

  7. iptables实现负载均衡

    例子: iptables -t nat -A PREROUTING -d 10.192.0.65/32 -p tcp -m tcp --dport 8080 -m statistic --mode n ...

  8. ubuntu14.04禁用guest用户登录

    打开终端(ctrl+alt+t) sudo  echo -e "[SeatDefaults]\nallow-guest=false"  > /usr/share/lightd ...

  9. 样条函数 -- spline function

    一类分段(片)光滑.并且在各段交接处也有一定光滑性的函数.简称样条.样条一词来源于工程绘图人员为了将一些指定点连接成一条光顺曲线所使用的工具,即富有弹性的细木条或薄钢条.由这样的样条形成的曲线在连接点 ...

  10. win7远程桌面连接不上,解决办法

    来源于:http://jingyan.baidu.com/article/39810a23edc48bb637fda672.html 一般情况下,对WIN7的远程连接只需要5步即可完成远程连接的设置: ...