[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 ...
随机推荐
- Java中通过Array.sort()对数组从大到小排序
package com.itheimajavase; import java.util.Arrays; import java.util.Comparator; public class Day01 ...
- [Luogu P4145] 上帝造题的七分钟2 / 花神游历各国
题目链接 题目简要:我们需要一个能支持区间内每一个数开方以及区间求和的数据结构. 解题思路:说道区间修改区间查询,第一个想到的当然就是分块线段树.数据范围要用long long.本来我是看到区间这两个 ...
- ZROI NOI2019集训汇总
Day1 T1:已改但咕了(下午就补,没力气写了...) T2:未改 T3:未改 Day2 T1:在这里 T2:未改 T3:在这里 Day3 T1:在这里 T2:博客写在了本地,结果被数据清空了... ...
- 网络1911、1912 C语言第4次作业--函数批改总结
目录 网络1911.1912 C语言第4次作业--函数批改总结 一.评分规则 二.本次作业亮点 三.本次作业存在的问题 四.助教有话说 五.得分详情 网络1911.1912 C语言第4次作业--函数批 ...
- devstack 使用openstack命令报错 The request you have made requires authentication. (HTTP 401) Missing value auth-url required for auth plugin password
关联错误: The request you have made requires authentication. (HTTP 401) (Request-ID: req-88ad2cba-0f2d-4 ...
- 沙雕题目 来自luogu
P5316 恋恋的数学题 题目描述 现在恋恋正在处理的题目十分简单:现在有k (2≤k≤4)k \space (2\leq k\leq 4)k (2≤k≤4)个数,恋恋不知道它们分别是几,只知道它们两 ...
- ICEM—三分之一风扇
原视频下载地址:https://yunpan.cn/cSwYBI6sb9vHS 访问密码 9059
- golang list使用 双层 循环 删除 遍历
queue队列: import ( "container/list" "sync" ) type Queue struct { l *list.List m s ...
- ailoop2里面的1个待考察的,在ailoop3里面的操作。(先使用海巨人,不使用英雄技能召唤图腾的问题)
承接上一篇博客HearthBuddy Ai 调试实战2 在使用海巨人的时候,少召唤了一个图腾(费用是对的) 研究ailoop2里面4个待考察的,在ailoop3里面的后续操作.ailoop3一共有36 ...
- 数据库访问接口之ODBC
ODBC API 实现数据库操作的手段是句柄.在ODBC中,使用不同的句柄(HANDLE)来标志环境(environment).连接(Connection).语句(statement).描述符(des ...