bzoj 2118 墨墨的等式 - 图论最短路建模
墨墨突然对等式很感兴趣,他正在研究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 墨墨的等式 - 图论最短路建模的更多相关文章
- 【BZOJ 2118】 墨墨的等式(Dijkstra)
BZOJ2118 墨墨的等式 题链:http://www.lydsy.com/JudgeOnline/problem.php?id=2118 Description 墨墨突然对等式很感兴趣,他正在研究 ...
- BZOJ2118墨墨的等式[数论 最短路建模]
2118: 墨墨的等式 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1317 Solved: 504[Submit][Status][Discus ...
- 【BZOJ 2118】墨墨的等式
http://www.lydsy.com/JudgeOnline/problem.php?id=2118 最短路就是为了找到最小的$x$满足$x=k×a_{min}+d,0≤d<a_{min}$ ...
- [图论训练]BZOJ 2118: 墨墨的等式 【最短路】
Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在 ...
- 数论+spfa算法 bzoj 2118 墨墨的等式
2118: 墨墨的等式 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1283 Solved: 496 Description 墨墨突然对等式很感兴 ...
- 【BZOJ 2118】 2118: 墨墨的等式 (最短路)
2118: 墨墨的等式 Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求 ...
- bzoj 2118: 墨墨的等式
Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+-+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在 ...
- bzoj 2118: 墨墨的等式 spfa
题目: 墨墨突然对等式很感兴趣,他正在研究\(a_1x_1+a_2y_2+ ... +a_nx_n=B\)存在非负整数解的条件,他要求你编写一个程序,给定\(N,\{a_n\}\)以及\(B\)的取值 ...
- 【BZOJ2118】墨墨的等式(最短路)
[BZOJ2118]墨墨的等式(最短路) 题面 BZOJ 洛谷 题解 和跳楼机那题是一样的. 只不过走的方式从\(3\)种变成了\(n\)种而已,其他的根本没有区别了. #include<ios ...
随机推荐
- postgresql shell发起select操作报错ERROR: relation "tablename" does not exist
最近安装了一套clourdera manager,其中hive元数据保存在postgresql中,因为今天想看一下hive的元数据信息,就登录了psql,连接到hive元数据库,发起select操作, ...
- angular2新建的项目上传github
前提1.git配置好,参见博文 git常用操作 2.angular2安装配置好,这个网上有很多教程,就不多说了,参见angular2快速起步 需要注意的是,大部分问题都是网络问题. 3.新建一个ng项 ...
- 字符编码笔记:ASCII,Unicode和UTF-8(转载)
注:我注释的地方有 add by zhj.另Unicode.UTF-8.GB2312查询http://www.2fz1.com/so/ 在python中,a.decode(xxx)就是把str类型的字 ...
- 启动yarn
$cd /app/hadoop/hadoop-2.2.0/sbin $./start-yarn.sh
- property:get、set
property属性:自动调用get.set方法 每次调用隐藏的数据,都用get和set方法写的字符太长,就用property解决. 方法一:使用property()函数升级get.set方法 源 ...
- 为什么*p++等于*(p++)?
你要先搞懂i++与++i的区别.i++是先赋值再自增,对于指针也是一样的.所以*p++是先取值,然后p再自增.加个括号还是一样的,*(p++)括号里面的内容还是p++,所以还是要先取值然后p再自增. ...
- 让人抓狂的MySQL安装-8.0.12版本
今天一个下午就做了一件事,把MySQL安装成功,安装的过程让人很狂躁.于是一边骂,一边查错,才把这个软件给安装成功了. 详细的安装步骤,这里就不赘述了.参见https://blog.csdn.net/ ...
- 使用navicat mysql 远程连接数据库
远程连接数据库,假设两台主机上都有navicat 客户端 远程主机A ip地址:192.168.100.91 ,port 3306,数据库用户名 rootA 密码 123456A 本地主 ...
- Windows下Ionic Android开发环境搭建
转自 http://www.itwap.net/ArticleContent.aspx?id=26 来源: itwap.net 作者: 词略 时间: 2015-4-2 16:57:28 (一)Ioni ...
- CE寻找游戏基址
什么是游戏基址? 游戏基址是保持恒定的两部分内存地址的一部分并提供一个基准点,从这里可以计算一个字节数据的位置.基址伴随着一个加到基上的偏移值来确定信息准确的位置(绝对地址). 全局基址 一级基址 二 ...