传送门

好神啊。。

需要用非负数个a1,a2,a3...an来凑出B

可以知道,如果一个数x能被凑出来,那么x+a1,x+a2.......x+an也都能被凑出来

那么我们只需要选择a1~an中任意一个的a,可以求出在%a下的每个数最小需要多少才能凑出来

这样我们选择一个最小的a,速度更快,令m=min(a[k]) 1 <= k <= n

然后建模,i向(i+a[j])%m连一条权值为a[j]的边

跑一边最短路就可以了

然后需要求Bmin~Bmax中的解

只需要ans(Bmax)-ans(Bmin)即可

注意a[i]==0的点。。。。

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 6000001
#define LL long long using namespace std; int n, cnt;
int head[N], to[N], next[N];
LL L, R, ans, dis[N], m = ~(1 << 31), a[21], val[N];
bool vis[N];
queue <int> q; inline LL read()
{
LL x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline void add(int x, int y, LL z)
{
to[cnt] = y;
val[cnt] = z;
next[cnt] = head[x];
head[x] = cnt++;
} inline void spfa()
{
int i, u, v;
for(i = 0; i < m; i++) dis[i] = 1e13;
q.push(0);
dis[0] = 0;
while(!q.empty())
{
u = q.front();
vis[u] = 0;
q.pop();
for(i = head[u]; ~i; i = next[i])
{
v = to[i];
if(dis[v] > dis[u] + val[i])
{
dis[v] = dis[u] + val[i];
if(!vis[v])
{
vis[v] = 1;
q.push(v);
}
}
}
}
} inline LL query(LL x)
{
int i;
LL ans = 0;
for(i = 0; i < m; i++)
if(dis[i] <= x)
ans += (x - dis[i]) / m + 1;
return ans;
} int main()
{
LL x, y;
int i, j;
n = read();
L = read();
R = read();
memset(head, -1, sizeof(head));
for(i = 1; i <= n; i++)
{
a[i] = read();
if(!a[i])
{
i--, n--;
continue;
}
m = min(m, a[i]);
}
for(i = 0; i < m; i++)
for(j = 1; j <= n; j++)
add(i, (i + a[j]) % m, a[j]);
spfa();
printf("%lld\n", query(R) - query(L - 1));
return 0;
}

  

最新文章

  1. Asterisk manager API(AMI)文档(中文版)
  2. MySQL分布式集群之MyCAT(转)
  3. ui-router中的锚点问题(angular中的锚点问题)
  4. 手机抓包xcode自带命令行工具配合wireshark实现
  5. Android Studio 设置不自动缩进匿名内部类
  6. 在相同的主机上创建一个duplicate数据库
  7. mysql cluster 名词概念解读
  8. Ant编译和部署java web项目
  9. WordPress RokMicroNews插件‘thumb.php’ 多个安全漏洞
  10. hive运行query语句时提示错误:org.apache.hadoop.ipc.RemoteException: java.io.IOException: java.io.IOException:
  11. LVS+Keepalived实现MySQL从库读操作负载均衡
  12. HDU 5775 Bubble Sort
  13. 一文让你从此告别HTTP乱码(二)Response篇
  14. hi3531调用sil9024的驱动
  15. 伙伴系统之避免碎片--Linux内存管理(十六)
  16. JAVA进阶5
  17. php+mysql简单的添加和删除小案例
  18. Bash on Ubuntu on Windows 到底想干啥?apt update又能解决啥问题?
  19. Swift学习笔记4
  20. STM32F1-workarea : how to drive a WS2812 RGB LED using PWM and DMA

热门文章

  1. SQLServer同一实例下事务操作
  2. VS 2013如何编译ASM文件
  3. Hibernate 中批量处理数据
  4. JAVA web项目转客户端(nativefier)
  5. RedHat7搭建KVM虚拟机
  6. 三. python面向对象
  7. 企业版https
  8. 简单css动画 fadeIn fadeOut flash
  9. MySQL 上移/下移/置顶
  10. MariaDB数据库(五)