BZOJ2118 墨墨的等式 【最短路】
题目链接
题解
orz竟然是最短路
我们去\(0\)后取出最小的\(a[i]\),记为\(p\),然后考虑模\(p\)下的\(B\)
一个数\(i\)能被凑出,那么\(i + p\)也能被凑出
所以我们只需找出最小的凑出\(i\)的代价
我们如果将同余下的和看作点,那么加上一个数就相当于在点间转移的边
所以我们只需跑最短路即可求出每个\(i\)的最小代价,然后就可以计算\(Bmin\)和\(Bmax\)以内分别有多少个\(i\)
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 500005,maxm = 5000005;
const LL INF = 100000000000000001ll;
inline LL read(){
LL out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
struct node{
int u; LL d;
};
inline bool operator <(const node& a,const node& b){
return a.d > b.d;
}
inline bool operator ==(const node& a,const node& b){
return a.u == b.u && a.d == b.d;
}
struct Heap{
priority_queue<node> a,b;
void ck(){while (!b.empty() && a.top() == b.top()) a.pop(),b.pop();}
int size(){return a.size() - b.size();}
node top(){ck(); node x = a.top(); a.pop(); return x;}
void del(node x){ck(); b.push(x);}
void ins(node x){ck(); a.push(x);}
}H;
int N,a[maxn],P;
LL d[maxn]; int vis[maxn];
int h[maxn],ne;
struct EDGE{int to,nxt,w;}ed[maxm];
inline void build(int u,int v,int w){
ed[++ne] = (EDGE){v,h[u],w}; h[u] = ne;
}
void work(){
for (int i = 0; i < P; i++){
for (int j = 1; j <= N; j++)
build(i,(i + a[j]) % P,a[j]);
}
for (int i = 1; i < P; i++) d[i] = INF;
d[0] = 0; H.ins((node){0,d[0]}); vis[0] = true;
node u;
while (H.size()){
u = H.top();
Redge(u.u) if (!vis[to = ed[k].to] && d[to] > d[u.u] + ed[k].w){
if (d[to] != INF) H.del((node){to,d[to]});
d[to] = d[u.u] + ed[k].w;
H.ins((node){to,d[to]});
}
}
}
int main(){
N = read(); LL L = read(),R = read(); P = INF;
REP(i,N){
a[i] = read();
if (!a[i]) i--,N--;
}
if (!N){
if (L) puts("0");
else puts("1");
return 0;
}
REP(i,N) P = min(P,a[i]);
work();
L--;
LL ansl = 0,ansr = 0;
for (int i = 0; i < P; i++){
if (d[i] <= L){
ansl++;
ansl += (L - d[i]) / P;
}
if (d[i] <= R){
ansr++;
ansr += (R - d[i]) / P;
}
}
printf("%lld\n",ansr - ansl);
return 0;
}
BZOJ2118 墨墨的等式 【最短路】的更多相关文章
- 【BZOJ2118】墨墨的等式(最短路)
[BZOJ2118]墨墨的等式(最短路) 题面 BZOJ 洛谷 题解 和跳楼机那题是一样的. 只不过走的方式从\(3\)种变成了\(n\)种而已,其他的根本没有区别了. #include<ios ...
- 【BZOJ2118】墨墨的等式 最短路
[BZOJ2118]墨墨的等式 Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值 ...
- BZOJ2118:墨墨的等式(最短路)
Description 墨墨突然对等式很感兴趣,他正在研究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可以使等式存在 ...
- BZOJ2118墨墨的等式[数论 最短路建模]
2118: 墨墨的等式 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1317 Solved: 504[Submit][Status][Discus ...
- Bzoj2118 墨墨的等式
Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1488 Solved: 578 Description 墨墨突然对等式很感兴趣,他正在研究a1x1+ ...
- bzoj 2118 墨墨的等式 - 图论最短路建模
墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在非负整数解. Input ...
- 【BZOJ 2118】 2118: 墨墨的等式 (最短路)
2118: 墨墨的等式 Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求 ...
随机推荐
- python中 列表常用的操作
列表可以装大量的数据,不限制数据类型,表示方式:[]:列表中的元素用逗号隔开. lst = [] #定义一个空列表 lst = ["Tanxu",18,"女", ...
- Leecode刷题之旅-C语言/python-104二叉树最大深度
/* * @lc app=leetcode.cn id=104 lang=c * * [104] 二叉树的最大深度 * * https://leetcode-cn.com/problems/maxim ...
- Leecode刷题之旅-C语言/python-67二进制求和
/* * @lc app=leetcode.cn id=67 lang=c * * [67] 二进制求和 * * https://leetcode-cn.com/problems/add-binary ...
- PATA1034题解
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624 参考:算法笔记(胡凡)10.3.1 # ...
- POJ1426 Find The Multiple 解题报告
参考:http://www.cnblogs.com/ACShiryu/archive/2011/07/24/2115356.html #include <iostream> #includ ...
- ABAP CDS ON HANA-(10)項目結合して一つ項目として表示
Numeric Functions ABS(arg) CEIL(arg) DIV(arg1, arg2) DIVISION(arg1, arg2, dec) FLOOR(arg) MOD(arg1, ...
- log报错: Caused by: java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.
报错: 解决方式: 1.登录数据库查看错误原因 结果发现账号无法正常登录出现账号被锁定的错误. 2.如何账号解锁? 用sys系统管理员账号登录数据库 SQL> alter user 用户名 ac ...
- 《python核心编程第二版》第1章练习
1–1. 安装 Python.请检查 Python 是否已经安装到你的系统上,如果没有,请下载并 安装它 略 1–2. 执行 Python.有多少种运行 Python 的不同方法?你喜欢哪一种?为什 ...
- Oracle修改表字段类型(number-->varchar2(len)),亲测可用
思路: --新建临时表以存储正确的顺序create table A_2 as select (column1,colum2,……A表中的顺序) from A_1 ; --删除表A_1drop tabl ...
- 怎么用Q-Q图验证数据集的分布
样本数据集在构建机器学习模型的过程中具有重要的作用,样本数据集包括训练集.验证集.测试集,其中训练集和验证集的作用是对学习模型进行参数择优,测试集是测试该模型的泛化能力. 正负样本数据集符合独立同分布 ...