题目链接

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;
}

最新文章

  1. 纯JS判断各种浏览器类型及版本.
  2. POJ2115 C Looooops[扩展欧几里得]
  3. MongoDB初步(一)
  4. django中抽象基类的Foreignkey的定义
  5. WPF下递归生成树形数据绑定到TreeView上
  6. python分页和session和计算时间差
  7. 【HDOJ】1867 A + B for you again
  8. swift小结01--基础篇
  9. 磁盘性能指标--IOPS 理论
  10. [Spark内核] 第33课:Spark Executor内幕彻底解密:Executor工作原理图、ExecutorBackend注册源码解密、Executor实例化内幕、Executor具体工作内幕
  11. Quartz简单案例
  12. Springboot配置多数据源(Mysql和Orcale)--(Idea Maven JDBCTemplate支持下的)
  13. 《Tornado介绍》—— 读后总结
  14. 016 SpringMVC中重定向
  15. POJ 1456 - Supermarket - [贪心+小顶堆]
  16. Java并发(一)并发编程的挑战
  17. BZOJ 4361 isn | DP 树状数组
  18. django model form 保存方法 django-rest-framework save 修改某一项值 方法
  19. C 六学家的困惑 【YY】 (2019年华南理工大学程序设计竞赛(春季赛))
  20. 【数组】—冒泡排序&amp;&amp;选择排序---【巷子】

热门文章

  1. 使用CSS3制作首页登录界面实例
  2. Linux Shell常用命令(长期更新)
  3. Angular简单总结
  4. vm 中 centOS 7 固定ip设置
  5. 如何用Python做自动化特征工程
  6. linux下SVN CVS命令大全
  7. 打印N个真值的所有真值组合
  8. dotnet core 数据库
  9. C++11中default的使用
  10. Python字符串处理:过滤字符串中的英文与符号,保留汉字