题目链接

BZOJ2118

题解

orz竟然是最短路

我们去\(0\)后取出最小的\(a[i]\),记为\(p\),然后考虑模\(p\)下的\(B\)

一个数\(i\)能被凑出,那么\(i + p\)也能被凑出

所以我们只需找出最小的凑出\(i\)的代价

我们如果将同余下的和看作点,那么加上一个数就相当于在点间转移的边

所以我们只需跑最短路即可求出每个\(i\)的最小代价,然后就可以计算\(Bmin\)和\(Bmax\)以内分别有多少个\(i\)

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<cstdio>
  5. #include<queue>
  6. #include<cmath>
  7. #include<map>
  8. #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
  9. #define REP(i,n) for (int i = 1; i <= (n); i++)
  10. #define mp(a,b) make_pair<int,int>(a,b)
  11. #define cls(s) memset(s,0,sizeof(s))
  12. #define cp pair<int,int>
  13. #define LL long long int
  14. using namespace std;
  15. const int maxn = 500005,maxm = 5000005;
  16. const LL INF = 100000000000000001ll;
  17. inline LL read(){
  18. LL out = 0,flag = 1; char c = getchar();
  19. while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
  20. while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
  21. return out * flag;
  22. }
  23. struct node{
  24. int u; LL d;
  25. };
  26. inline bool operator <(const node& a,const node& b){
  27. return a.d > b.d;
  28. }
  29. inline bool operator ==(const node& a,const node& b){
  30. return a.u == b.u && a.d == b.d;
  31. }
  32. struct Heap{
  33. priority_queue<node> a,b;
  34. void ck(){while (!b.empty() && a.top() == b.top()) a.pop(),b.pop();}
  35. int size(){return a.size() - b.size();}
  36. node top(){ck(); node x = a.top(); a.pop(); return x;}
  37. void del(node x){ck(); b.push(x);}
  38. void ins(node x){ck(); a.push(x);}
  39. }H;
  40. int N,a[maxn],P;
  41. LL d[maxn]; int vis[maxn];
  42. int h[maxn],ne;
  43. struct EDGE{int to,nxt,w;}ed[maxm];
  44. inline void build(int u,int v,int w){
  45. ed[++ne] = (EDGE){v,h[u],w}; h[u] = ne;
  46. }
  47. void work(){
  48. for (int i = 0; i < P; i++){
  49. for (int j = 1; j <= N; j++)
  50. build(i,(i + a[j]) % P,a[j]);
  51. }
  52. for (int i = 1; i < P; i++) d[i] = INF;
  53. d[0] = 0; H.ins((node){0,d[0]}); vis[0] = true;
  54. node u;
  55. while (H.size()){
  56. u = H.top();
  57. Redge(u.u) if (!vis[to = ed[k].to] && d[to] > d[u.u] + ed[k].w){
  58. if (d[to] != INF) H.del((node){to,d[to]});
  59. d[to] = d[u.u] + ed[k].w;
  60. H.ins((node){to,d[to]});
  61. }
  62. }
  63. }
  64. int main(){
  65. N = read(); LL L = read(),R = read(); P = INF;
  66. REP(i,N){
  67. a[i] = read();
  68. if (!a[i]) i--,N--;
  69. }
  70. if (!N){
  71. if (L) puts("0");
  72. else puts("1");
  73. return 0;
  74. }
  75. REP(i,N) P = min(P,a[i]);
  76. work();
  77. L--;
  78. LL ansl = 0,ansr = 0;
  79. for (int i = 0; i < P; i++){
  80. if (d[i] <= L){
  81. ansl++;
  82. ansl += (L - d[i]) / P;
  83. }
  84. if (d[i] <= R){
  85. ansr++;
  86. ansr += (R - d[i]) / P;
  87. }
  88. }
  89. printf("%lld\n",ansr - ansl);
  90. return 0;
  91. }

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. Java源码解析——集合框架(一)——ArrayList

    ArrayList源码分析 ArrayList就是动态数组,是Array的复杂版本,它提供了动态的增加和减少元素.灵活的设置数组的大小. 一.类声明 public class ArrayList< ...

  2. Ubuntu 16.04 swoole扩展安装注意!!!

    前言:目前很多项目估计常常会用到swoole扩展,如个人使用Ubuntu虚拟机安装扩展,这里总结一下遇到的问题: 一.先保证服务器时间同步当前地区时间,如北京时间: 1.设定时区 如:设定时区:dpk ...

  3. 微信小程序使用相机

    <view class="page-body"> <view class="page-body-wrapper"> <camera ...

  4. mysql 优化like查询

    1. like %keyword    索引失效,使用全表扫描.但可以通过翻转函数+like前模糊查询+建立翻转函数索引=走翻转函数索引,不走全表扫描. 2. like keyword%    索引有 ...

  5. python 排列组合

    笛卡尔积(product): 假设集合A={a, b},集合B={0, 1, 2},则两个集合的笛卡尔积为{(a, 0), (a, 1), (a, 2), (b, 0), (b, 1), (b, 2) ...

  6. 静态栈抽象数据类型stack实现

    #include<stdio.h> #include<stdbool.h> #include<stdlib.h> #define MAX_STACK_SIZE 10 ...

  7. python入门——Anaconda安装

    初学Python,可以选择python原始的IDE,但原始的IDE在使用过程中需要自己安装各种包,个人觉得初学者不需要将时间花在这些上面,而是应该直接学习python程序,这些比较杂的事情可以在以后的 ...

  8. node 动态页面渲染

    代码: 'use strict' const express = require('express'); const consoldiate = require('consolidate'); con ...

  9. Python3爬虫(三)请求库的使用之urllib

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.urllib库: 1. 是Python内置的HTTP请求库 2. 在Python2中,由urllib和urll ...

  10. 怎么防止别人动态在你程序生成代码(怎么防止别人反编译你的app)

    1.本地数据加密 iOS应用防反编译加密技术之一:对NSUserDefaults,sqlite存储文件数据加密,保护帐号和关键信息 2.URL编码加密 iOS应用防反编译加密技术之二:对程序中出现的U ...