墨墨突然对等式很感兴趣,他正在研究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。


  直接说正解,找到ai中的一个数(通常找最小的那一个,这样可以降低时间复杂度,至于为什么,看完应该就知道了),暂且设它为m吧。

  我们知道如果存在一个x,它能够被凑出来,那么(x + m), (x + 2m), ...都可以被凑出来。现在我们要找到这么一个最小的x。先建立一些点0,1,2,...,m - 1,表示满足条件的B模m的值。暂时先不考虑如何求出一次Bmin ~ Bmax中满足条件的B,因为可以用0到Bmax中的方案数减去0到Bmin - 1中的方案数。每个点连n - 1条边,第i个点的第j条边连向第(i + aj) % m个点,边权为aj

  看似有些跑题了,现在来讲讲它在题目中的含义。现在我们希望求到所有满足条件的最小的x,我们知道,当B模m的值为0时,B最小为0。同时我们可以用这个推出与它相邻的点代表的最小的B。再仔细想想,多推一下,这不是等价于求最短路吗?也就是说,从节点0出发,到达i的距离表示,最小的B模m的值为i的B的值。

  最后计算一下方案数(相信你会做)就好了。还有特殊处理当某个a等于0的时候,不然会整数被零除,然后无限RE。另外,贡献一发wa,建图注意是单向的,减个aj就不知道是不是满足了,笑。

Code

 /**
* bzoj
* Problem#2118
* Accepted
* Time:2888ms
* Memory:93712k
*/
#include<iostream>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<ctime>
#include<map>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#ifndef WIN32
#define AUTO "%lld"
#else
#define AUTO "%I64d"
#endif
using namespace std;
typedef bool boolean;
#define inf 0xfffffff
#define smin(a, b) (a) = min((a), (b))
#define smax(a, b) (a) = max((a), (b))
template<typename T>
inline boolean readInteger(T& u) {
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-') {
aFlag = -;
x = getchar();
}
for(u = x - ''; isdigit((x = getchar())); u = u * + x - '');
u *= aFlag;
ungetc(x, stdin);
return true;
} ///map template starts
typedef class Edge{
public:
int end;
int next;
int w;
Edge(const int end = , const int next = , const int w = ):end(end), next(next), w(w) { }
}Edge; typedef class MapManager{
public:
int ce;
int *h;
Edge *edge;
MapManager() { }
MapManager(int points, int limit):ce() {
h = new int[(const int)(points + )];
edge = new Edge[(const int)(limit + )];
memset(h, , sizeof(int) * (points + ));
}
inline void addEdge(int from, int end, int w) {
edge[++ce] = Edge(end, h[from], w);
h[from] = ce;
}
Edge& operator [] (int pos) {
return edge[pos];
}
}MapManager;
#define m_begin(g, i) (g).h[(i)]
///map template ends int n;
long long bmin, bmax;
MapManager g;
int* a;
int moder = inf; inline void init() {
readInteger(n);
readInteger(bmin);
readInteger(bmax);
a = new int[(const int)(n + )];
for(int i = ; i <= n; i++) {
readInteger(a[i]);
if(a[i] == ) {
i--, n--;
continue;
}
smin(moder, a[i]);
}
} boolean *visited;
long long* dis;
queue<int> que;
inline void spfa(int s) {
visited = new boolean[(const int)(moder)];
dis = new long long[(const int)(moder)];
memset(visited, false, sizeof(boolean) * (moder));
memset(dis, 0x7f, sizeof(long long) * (moder));
dis[s] = ;
que.push(s);
while(!que.empty()) {
int e = que.front();
que.pop();
visited[e] = false;
for(int i = m_begin(g, e); i != ; i = g[i].next) {
int &eu = g[i].end;
if(dis[e] + g[i].w < dis[eu]) {
dis[eu] = dis[e] + g[i].w;
if(!visited[eu]) {
que.push(eu);
visited[eu] = true;
}
}
}
}
} inline long long calc(long long x) {
long long ret = ;
for(int i = ; i < moder; i++) {
if(dis[i] <= x)
ret += (x - dis[i]) / (long long)moder + ;
}
return ret;
} inline void solve() {
g = MapManager(moder, moder * n * );
for(int i = ; i < moder; i++) {
for(int j = ; j <= n; j++) {
if(a[j] == moder) continue;
g.addEdge(i, (i + a[j]) % moder, a[j]);
}
}
spfa();
long long res = calc(bmax) - calc(bmin - );
printf(AUTO, res);
} int main() {
init();
solve();
return ;
}

bzoj 2118 墨墨的等式 - 图论最短路建模的更多相关文章

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

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

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

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

  3. 【BZOJ 2118】墨墨的等式

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

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

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

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

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

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

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

  7. bzoj 2118: 墨墨的等式

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

  8. bzoj 2118: 墨墨的等式 spfa

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

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

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

随机推荐

  1. Oracle HA 之 测试RAC的功能

    作用:在oracle数据库instance级别的冗余,其中只要有一个instance可用即可保证可用性,但是不能保准数据级别的错误. 数据库文件需要放置在共享存储上,理论上一个实例对应一个数据库,实例 ...

  2. AT2043 AND Grid 构造

    正解:构造 解题报告: 传送门传送门! 这题psj讲了俩做法,一个是最常见的解法,还一种还不知道484对的QAQ 然后先把psj讲的不知正确性的做法港下QwQ 大概就是说,第一个图,先把底给染完 然后 ...

  3. ICMP报文

    类型:表示ICMP消息类型 代码:表示同一消息的不同信息 其他是时间戳或者标识符及序列号 类型 编码 描述   0 0 Echo Reply 3 0 网络不可达 3 1 主机不可达 3 2 协议不可达 ...

  4. 通过PyPI镜像安装Python包

    有时,我们直接用pip install package_name安装包时,下载很慢,我们可以尝试从PyPI镜像下载包. 这里是镜像列表和目前的状态: http://www.pypi-mirrors.o ...

  5. IO流(4)重命名

    重命名功能:public boolean renameTo(File dest) * 如果路径名相同,就是改名. * 如果路径名不同,就是改名并剪切. * * 路径以盘符开始:绝对路径 c:\\a.t ...

  6. Dubbo简单环境搭建

    Dubbo服务的发展和作用: 首先,看下一般网站架构随着业务的发展,逻辑越来越复杂,数据量越来越大,交互越来越多之后的常规方案演进历程. 其次,当服务越来越多之后,我们需要做哪些服务治理? 最后,是d ...

  7. myeclipse maven工程调试调试

    1:使用了maven带的tomca插件进行启动.下面讲一下如何进行调试程序 2:在程序中打断点 3:选择>debug>debug configuration 在goals处添加启动命令 4 ...

  8. [js]js中原型的继承

    js继承01 思路: 单例/工厂/构造函数--演进到原型 搞清原型结构 原型继承 模拟系统原型继承 实现自己的继承 观察原型继承特点 演进到原型链这一步 //单例模式: 防止变量名冲突: // 思路: ...

  9. [lr] 直方图

    直方图基础知识 • 直方图的特征和作用 ▪ 直方图的x轴从左到右代表亮度逐渐增加,即从最暗到最亮:y轴代表某个亮度值下颜色像素的多少(密度). ▪ 直方图由红绿蓝三种颜色组成,分别表示红绿蓝通道:其中 ...

  10. php截取制定长度字符串

    /** +---------------------------------------------------------- * 字符串截取,支持中文和其他编码 +----------------- ...