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的更多相关文章

  1. B20J_2007_[Noi2010]海拔_平面图最小割转对偶图+堆优化Dij

    B20J_2007_[Noi2010]海拔_平面图最小割转对偶图+堆优化Dij 题意:城市被东西向和南北向的主干道划分为n×n个区域.城市中包括(n+1)×(n+1)个交叉路口和2n×(n+1)条双向 ...

  2. [CF1209F]Koala and Notebook_堆优化dij

    Koala and Notebook 题目链接:https://codeforces.com/contest/1209/problem/F 数据范围:略. 题解: 开始的时候看错题了....莫名其妙多 ...

  3. 【洛谷P1462】【二分+堆优化dij】

    题目描述 在艾泽拉斯,有n个城市.编号为1,2,3,...,n. 城市之间有m条双向的公路,连接着两个城市,从某个城市到另一个城市,会遭到联盟的攻击,进而损失一定的血量. 每次经过一个城市,都会被收取 ...

  4. 【模板】堆优化 + dij +pair 存储

    就是短 感谢Cptraserdalao的博客 #include<bits/stdc++.h> using namespace std; struct node { int val,num; ...

  5. 链式前向星实现的堆优化dij求最短路模板

    #include<cstdio> #include<string> #include<cstdlib> #include<cmath> #include ...

  6. 堆优化dij

    #include<iostream> #include<cstdio> #include<queue> using namespace std; ],head[], ...

  7. 2017多校第9场 HDU 6166 Senior Pan 堆优化Dij

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6166 题意:给你一个有向图,然后给你k个点,求其中一个点到另一个点的距离的最小值. 解法:枚举二进制位 ...

  8. [CF1005F]Berland and the Shortest Paths_最短路树_堆优化dij

    Berland and the Shortest Paths 题目链接:https://www.codeforces.com/contest/1005/problem/F 数据范围:略. 题解: 太鬼 ...

  9. dij+堆优化

    写这个dij+堆优化的原因是有些地方卡SPFA,只能搞这个: 香甜的奶油: #include<iostream> #include<cstdio> #include<cs ...

随机推荐

  1. Linq to AD

    輕鬆找出域中所有用戶,組,而且還可以更改 LINQ to Active Directory https://linqtoad.codeplex.com/ LINQ to LDAP http://lin ...

  2. python+opencv 图像预处理

    一 python 生成随机字符串序列+ 写入到图片上 from PIL import Image,ImageDraw,ImageFont import numpy as np import rando ...

  3. 一图了解DLL和SYS的区别

    The following diagram shows the device node, kernel-mode device stack, and the user-mode device stac ...

  4. 转义字符\'和\"的使用示例

    /* 转义字符\'和\"的使用示例 */ #include <stdio.h> int main(void) { printf("关于字符串常量和字符常量.\n&quo ...

  5. 进程 | 线程 | 当Linux多线程遭遇Linux多进程

    背景 本文并不是介绍Linux多进程多线程编程的科普文,如果希望系统学习Linux编程,可以看[<Unix环境高级编程>第3版] 本文是描述多进程多线程编程中遇到过的一个坑,并从内核角度分 ...

  6. 通过id获取指定元素内容(标签里面的 标签内容获取)

    html页面如下 <tr style="background-color:#fff;"> <td colspan="2" align=left ...

  7. OpenLDAP管理命令详解

    一.OpenLDAP命令汇总 ldapsearch:搜索 OpenLDAP 目录树条目. ldapadd:通过 LDIF 格式,添加目录树条目. ldapdelete:删除 OpenLDAP 目录树条 ...

  8. 关于POW机制及POW难度调节机制

    工作量证明,英文为proof of work,通过或与计算,计算出一个满足规则的随机数,即获得本次记账权,发出本轮需要记录的数据,全网其他节点验证后一起存储.简单理解就是一份证明,用来确认你做过一定量 ...

  9. Android之View的内容

    View的事件体系 本章介绍View的事件分发和滑动冲突问题的解决方案. 3.1 view的基础知识 View的位置参数.MotionEvent和TouchSlop对象.VelocityTracker ...

  10. 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)

    剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...