[CF1146D]Frog Jumping_exgcd_堆优化dij
Frog Jumping
题目链接:http://codeforces.com/contest/1146/problem/D
数据范围:略。
题解:
首先发现,如果$x\ge a +b$,那么所有的$Num | gcd(a,b)$都可以取到。
这是显然的因为我们可以保证最右端点在$a+b$内。
那么我们只需要考虑小于$x$的部分。
可以暴力建边,跑出当前点需要的最右端点的最小值,用spfa或者堆优化dij都行。
代码:
#include <bits/stdc++.h>
#define N 1000010
using namespace std;
typedef long long ll;
int tag[N];
int head[N], to[N << 1], nxt[N << 1], tot;
inline void add(int x, int y) {
to[ ++ tot] = y;
nxt[tot] = head[x];
head[x] = tot;
}
int dis[N];
bool vis[N];
queue <int> q;
void spfa(int a, int b) {
memset(dis, 0x3f, sizeof dis);
dis[0] = 0;
q.push(0);
vis[0] = 1;
while (!q.empty()) {
int x = q.front();
vis[x] = false;
q.pop();
int pre = x - b, aft = x + a;
if (pre >= 0 && max(dis[x], pre) < dis[pre]) {
dis[pre] = max(dis[x], pre);
if (!vis[pre]) {
q.push(pre);
vis[pre] = true;
}
}
if (aft <= a + b && max(dis[x], aft) < dis[aft]) {
dis[aft] = max(dis[x], aft);
if (!vis[aft]) {
q.push(aft);
vis[aft] = true;
}
}
}
}
int f[N];
int main() {
int x, a, b;
cin >> x >> a >> b ;
spfa(a, b);
// puts("Fuck");
int d = __gcd(a, b);
if (x <= a + b) {
for (int i = 0; i <= x; i += d) {
if (dis[i] != 0x3f3f3f3f) {
tag[dis[i]] ++ ;
}
}
for (int i = 1; i <= x; i ++ ) {
tag[i] += tag[i - 1];
}
long long ans = 0;
for (int i = 0; i <= x; i ++ ) {
ans += tag[i];
}
cout << ans << endl ;
return 0;
}
// puts("A");
for (int i = 0; i <= a + b; i += d) {
// printf("%d\n", dis[i]);
if (dis[i] != 0x3f3f3f3f) {
tag[dis[i]] ++ ;
}
}
// puts("B");
for (int i = 1; i <= a + b; i ++ ) {
tag[i] += tag[i - 1];
}
// puts("C");
ll ans = 0;
for (int i = 0; i <= a + b; i ++ ) {
ans += tag[i];
}
// cout << ans << endl ;
int id = 0;
for (int i = a + b + 1; i <= x; i ++ ) {
if (i % d == 0) {
id = i;
break;
}
ans += i / d + 1;
// cout << ans << endl ;
}
if (!id) {
cout << ans << endl ;
return 0;
}
// cout << id << endl ;
int bg = id / d, ed = x / d;
ans += (ll)(bg + ed + 1) * (ed - bg) / 2 * d;
ans += (ll)(x - (ll)ed * d + 1) * (ed + 1);
cout << ans << endl ;
return 0;
}
[CF1146D]Frog Jumping_exgcd_堆优化dij的更多相关文章
- B20J_2007_[Noi2010]海拔_平面图最小割转对偶图+堆优化Dij
B20J_2007_[Noi2010]海拔_平面图最小割转对偶图+堆优化Dij 题意:城市被东西向和南北向的主干道划分为n×n个区域.城市中包括(n+1)×(n+1)个交叉路口和2n×(n+1)条双向 ...
- [CF1209F]Koala and Notebook_堆优化dij
Koala and Notebook 题目链接:https://codeforces.com/contest/1209/problem/F 数据范围:略. 题解: 开始的时候看错题了....莫名其妙多 ...
- 【洛谷P1462】【二分+堆优化dij】
题目描述 在艾泽拉斯,有n个城市.编号为1,2,3,...,n. 城市之间有m条双向的公路,连接着两个城市,从某个城市到另一个城市,会遭到联盟的攻击,进而损失一定的血量. 每次经过一个城市,都会被收取 ...
- 【模板】堆优化 + dij +pair 存储
就是短 感谢Cptraserdalao的博客 #include<bits/stdc++.h> using namespace std; struct node { int val,num; ...
- 链式前向星实现的堆优化dij求最短路模板
#include<cstdio> #include<string> #include<cstdlib> #include<cmath> #include ...
- 堆优化dij
#include<iostream> #include<cstdio> #include<queue> using namespace std; ],head[], ...
- 2017多校第9场 HDU 6166 Senior Pan 堆优化Dij
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6166 题意:给你一个有向图,然后给你k个点,求其中一个点到另一个点的距离的最小值. 解法:枚举二进制位 ...
- [CF1005F]Berland and the Shortest Paths_最短路树_堆优化dij
Berland and the Shortest Paths 题目链接:https://www.codeforces.com/contest/1005/problem/F 数据范围:略. 题解: 太鬼 ...
- dij+堆优化
写这个dij+堆优化的原因是有些地方卡SPFA,只能搞这个: 香甜的奶油: #include<iostream> #include<cstdio> #include<cs ...
随机推荐
- ubuntu14.04重启网卡的三种方法
Linux重启网卡的三种方法: 一.network 利用root帐户 # service network restart 或者/etc/init.d/networking restart 二.ifdo ...
- Java并发指南6:Java内存模型JMM总结
本文转载自互联网,侵删 在前面的文章中我们介绍了Java并发基础和线程安全的概念,以及JMM内存模型的介绍,包括其定义的各种规则.同时我们也介绍了volatile在JMM中的实现原理,以及Lock ...
- pwn学习日记Day14 《程序员的自我修养》读书笔记
目标文件:计算机科学中存放目标代码的计算机文件,包含着机器代码,代码在运行时使用的数据,调试信息等,是从源代码文件产生程序文件这一过程的中间产物. 目标代码(objectcode)指计算机科学中编译器 ...
- Cesium中级教程6 - 3D Models 三维模型
3D Models 三维模型 本教程将教您如何通过Primitive API转换.加载和使用Cesium中的三维模型.如果你是Cesium的新用户,可能需要阅读三维模型部分的(空间数据可视化教程)[h ...
- Windows Bat 之For 循环
Windows Bat 之For 循环 1. For 循环基本用法. 1.1 格式 在cmd窗口中: FOR %variable IN (set) DO command [command-pa ...
- Servlet 的三种跳转方式
1.Forword request.getRequestDispatcher (“url”).forword (request,response) 是请求转发,也就是说,一个 Servlet 向当前的 ...
- 深入理解Flink ---- Metrics的内部结构
从Metrics的使用说起 Flink的Metrics种类有四种Counters, Gauges, Histograms和Meters. 如何使用Metrics呢? 以Counter为例, publi ...
- mongodb查询修改
//查 public StatisticsSchoolPracticeView findByUser(String userId,int statOrgType,int inDateType){ Qu ...
- 公司手机打卡app时间和百度时间差30秒解决
问题: 某天发现公司手机打卡app时间和百度时间差30秒解决 分析: nginx 192.168.0.23 外网 : 220.236.7.43 mysql主 192.168.0.2 ...
- 1-3 RHEL7操作系统的安装
RHEL7操作系统的安装 本节所讲内容: q RHEL7.2操作系统的安装 第1章 RHEL7系统安装 1.1 安装软件准备: 需要的软件如下: Vmware workstation 12(含注册码 ...