题目链接

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 墨墨的等式 【最短路】的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. Bzoj2118 墨墨的等式

    Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1488  Solved: 578 Description 墨墨突然对等式很感兴趣,他正在研究a1x1+ ...

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

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

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

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

随机推荐

  1. sublime3常用插件总结

    本人之前使用的是webstorm,后来改用sublime,渐渐的爱上了它的快!(自行体会) 正式介绍sublime3常用的一些插件,安装流程不再赘述! SublimeTmpl 创建常用文件初始模板,必 ...

  2. python面向对象-cs游戏示例

    #!/usr/local/bin/python3 # -*- coding:utf-8 -*- class Role(object): n = 123 # 类变量 name = "我是类na ...

  3. 基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(二)之cdev与read、write

    基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(二)之cdev与read.write 0. 导语 在上一篇博客里面,基于OMAPL138的字符驱动_GPIO驱动AD9833(一)之 ...

  4. go学习笔记-Data类型(Arrays, Slices and Maps)

    Data类型(Arrays, Slices and Maps) array array就是数组,定义方式如下: var arr [n]type 在[n]type中,n表示数组的长度,type表示存储元 ...

  5. 42-EF Core Migration

    1-常用命令 1-由于2.1版本有点不一样,不会自动创建ApplicationUser类,发现合并没效果.暂时略 增加一个字段 E:\coding\netcore\IdentitySample> ...

  6. vue2018年5月报错No parser and no file path given

    mac电脑直接: rm -rf node_modules rm package-lock.json npm install npm install prettier@~1.12.1 执行完这四个命令, ...

  7. LeetCode:20. Valid Parentheses(Easy)

    1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')',  ...

  8. Hibernate-ORM:07.Hibernate中的参数绑定

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客会讲解Hibernate中的参数绑定,就是相当于sql语句中的where后面的条件 一,讲解概述: 1 ...

  9. ORA-12546: TNS: 权限被拒绝(ORA - 12546 TNS: Permission Denied)

    这个问题上网一查大都是说权限之类的问题,本人在经过第二次折腾之后发现,其实是自己的Oracle客户端工具在破解过程中被自己用防火墙禁止访问网络了,自己还在另一篇博文里记录过,竟然忘光了,BS一下自己! ...

  10. Unity3d创建物体,寻找物体,加载物体,添加脚本

    GetCreateObject: using UnityEngine; public class GetCreateObject : MonoBehaviour { GameObject emptyG ...