bzoj 2118: 墨墨的等式 spfa
题目:
墨墨突然对等式很感兴趣,他正在研究\(a_1x_1+a_2y_2+ ... +a_nx_n=B\)存在非负整数解的条件,他要求你编写一个程序,给定\(N,\{a_n\}\)以及\(B\)的取值范围,求出有多少\(B\)可以使等式存在非负整数解。
题解:
首先我们发现 : 如果我们能够通过选取一些数凑到\(x\),那么我们肯定能够凑到$x + a_1 ,x + 2a_1 ,x + 3a_1, ... \(
所以我们考虑在\)mod a_1\(的剩余系下进行操作.
记\)f[x]\(表示取到可以用\)k*a_1 + x\(表示的数的最小的\)k$
这个dp我们可以直接利用最短路算法求解.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(ll &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const ll maxn = 500500;
const ll lim = maxn<<1;
ll a[maxn],dis[maxn],q[lim + 10],l,r,n;
bool inq[maxn];
void spfa(){
memset(dis,0x3f,sizeof dis);
l = 0;r = -1;
dis[0] = 0;q[++r] = 0;
inq[0] = true;
while(l <= r){
ll u = q[l++];
for(ll i=2;i<=n;++i){
ll v = (u + a[i]) % a[1];
if( dis[v] > dis[u] + (u+a[i])/a[1]){
dis[v] = dis[u] + (u+a[i])/a[1];
if(!inq[v]){
q[++r] = v;
inq[v] = true;
}
}
}inq[u] = false;
}
}
inline ll calc(ll x){
ll ret = 0;
for(ll i=0;i<a[1];++i){
ret += max((x/a[1] + ((x % a[1]) >= i)) - dis[i],0LL);
}return ret;
}
int main(){
ll L,R;read(n);read(L);read(R);
ll pos = 0;
for(ll i=1;i<=n;++i){
read(a[i]);
if(pos == 0 || a[pos] > a[i]) pos = i;
}swap(a[pos],a[1]);
if(a[1] == 0) return puts("0");
spfa();
printf("%lld\n",calc(R) - calc(L-1));
getchar();getchar();
return 0;
}
bzoj 2118: 墨墨的等式 spfa的更多相关文章
- 【BZOJ 2118】 墨墨的等式(Dijkstra)
BZOJ2118 墨墨的等式 题链:http://www.lydsy.com/JudgeOnline/problem.php?id=2118 Description 墨墨突然对等式很感兴趣,他正在研究 ...
- 【BZOJ 2118】墨墨的等式
http://www.lydsy.com/JudgeOnline/problem.php?id=2118 最短路就是为了找到最小的$x$满足$x=k×a_{min}+d,0≤d<a_{min}$ ...
- 数论+spfa算法 bzoj 2118 墨墨的等式
2118: 墨墨的等式 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1283 Solved: 496 Description 墨墨突然对等式很感兴 ...
- bzoj 2118 墨墨的等式 - 图论最短路建模
墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在非负整数解. Input ...
- 【BZOJ 2118】 2118: 墨墨的等式 (最短路)
2118: 墨墨的等式 Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求 ...
- bzoj 2118: 墨墨的等式
Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+-+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在 ...
- [图论训练]BZOJ 2118: 墨墨的等式 【最短路】
Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在 ...
- 【BZOJ2118】墨墨的等式(最短路)
[BZOJ2118]墨墨的等式(最短路) 题面 BZOJ 洛谷 题解 和跳楼机那题是一样的. 只不过走的方式从\(3\)种变成了\(n\)种而已,其他的根本没有区别了. #include<ios ...
- BZOJ2118: 墨墨的等式(同余类BFS)(数学转为图论题)
2118: 墨墨的等式 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2944 Solved: 1206[Submit][Status][Discu ...
随机推荐
- c/c++一些小知识点(特此总结)
---恢复内容开始--- ---恢复内容结束---
- And Then There Was One(约瑟夫环)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- RMQ with Shifts(线段树)
RMQ with Shifts Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u Pra ...
- 7.FactoryBean 和BeanFactory去区别
FactoryBean源码: /* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apach ...
- Springboot整合日志时候出现的问题
上图是问题,按照路径去找下,发现其实是jar包重复导致的! 在对应的项目上,右键--->属性(Properties)--->JavaBuild Path 然后选择Libraries 页签 ...
- php输出缓冲区
ob_start(); echo 'aaa'; $string = ob_get_contents(); file_put_contents('a.html', $string); ob_flush( ...
- Python菜鸟之路:Python基础-线程池注释
import sys import threading import Queue import traceback # 定义一些Exception,用于自定义异常处理 class NoResultsP ...
- 次小生成树Tree
次小生成树Treehttps://www.luogu.org/problemnew/show/P4180 题目描述 小C最近学了很多最小生成树的算法,Prim算法.Kurskal算法.消圈算法等等.正 ...
- 如何从统计中批量获取BD搜索关键词及对应的入口页面?
前面我们介绍了通过cnzz的访问明细获取到搜索关键词及对应的入口页面,但是从BD搜索进来的关键词无法完整显示,只能呈现一些bd图片搜索的关键词,这是因为百度宣布从去年5月开始逐渐取消了referer关 ...
- CentOS6安装DaoCloud加速器
天朝的网,你又不是不懂.我最爱的红杏最近也用不了了.FUCK GFW. 在这,我们使用DaoCloud的加速器,打开网址 https://dashboard.daocloud.io/mirror 找到 ...