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的取值范围,求 ...
随机推荐
- Java源码解析——集合框架(一)——ArrayList
ArrayList源码分析 ArrayList就是动态数组,是Array的复杂版本,它提供了动态的增加和减少元素.灵活的设置数组的大小. 一.类声明 public class ArrayList< ...
- Ubuntu 16.04 swoole扩展安装注意!!!
前言:目前很多项目估计常常会用到swoole扩展,如个人使用Ubuntu虚拟机安装扩展,这里总结一下遇到的问题: 一.先保证服务器时间同步当前地区时间,如北京时间: 1.设定时区 如:设定时区:dpk ...
- 微信小程序使用相机
<view class="page-body"> <view class="page-body-wrapper"> <camera ...
- mysql 优化like查询
1. like %keyword 索引失效,使用全表扫描.但可以通过翻转函数+like前模糊查询+建立翻转函数索引=走翻转函数索引,不走全表扫描. 2. like keyword% 索引有 ...
- python 排列组合
笛卡尔积(product): 假设集合A={a, b},集合B={0, 1, 2},则两个集合的笛卡尔积为{(a, 0), (a, 1), (a, 2), (b, 0), (b, 1), (b, 2) ...
- 静态栈抽象数据类型stack实现
#include<stdio.h> #include<stdbool.h> #include<stdlib.h> #define MAX_STACK_SIZE 10 ...
- python入门——Anaconda安装
初学Python,可以选择python原始的IDE,但原始的IDE在使用过程中需要自己安装各种包,个人觉得初学者不需要将时间花在这些上面,而是应该直接学习python程序,这些比较杂的事情可以在以后的 ...
- node 动态页面渲染
代码: 'use strict' const express = require('express'); const consoldiate = require('consolidate'); con ...
- Python3爬虫(三)请求库的使用之urllib
Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.urllib库: 1. 是Python内置的HTTP请求库 2. 在Python2中,由urllib和urll ...
- 怎么防止别人动态在你程序生成代码(怎么防止别人反编译你的app)
1.本地数据加密 iOS应用防反编译加密技术之一:对NSUserDefaults,sqlite存储文件数据加密,保护帐号和关键信息 2.URL编码加密 iOS应用防反编译加密技术之二:对程序中出现的U ...