链接

大意: 给定模数$p$, 假设当前在$x$, 则可以走到$x+1$, $x+p-1$, $x^{p-2}$ (mod p), 求任意一条从u到v不超过200步的路径

官方题解给了两个做法, 一个是直接双端搜索, 复杂度大概$O(\sqrt PlogP)$

还有一种方法是求出两条u->1, v->1长度不超过100的路径, 通过随机生成一个数x, 再对$(ux(mod P), u)$做欧几里得算法

操作2相当于减法, 操作三相当于交换

大概写了下双端搜索

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <set>
#include <map>
#include <queue>
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define hr cout<<'\n'
#define x first
#define y second
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int P;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
void exgcd(ll a,ll b,ll &d,ll &x,ll &y){b?exgcd(b,a%b,d,y,x),y-=a/b*x:x=1,y=0,d=a;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 4e5+10;
int a[N], b[N], c[N], vis[N], f[N], n, m, k, t;
vector<int> g[N]; map<int,pii> d1, d2; void ins(int x, int y, int op, map<int,pii> &d, queue<int> &q) {
if (d.count(x)) return;
d[x]={d[y].x+1,op};
q.push(x);
} void bfs(int x, map<int,pii> &d) {
queue<int> q;
q.push(x);
d[x]={0,-1};
REP(i,1,800000) {
if (q.empty()) break;
int u = q.front();q.pop();
ins((u+1)%P,u,1,d,q),ins((u+P-1)%P,u,2,d,q),ins(inv(u),u,3,d,q);
}
}
int buf[N]; void pr(int x, int y, map<int,pii> &d) {
*buf = 0;
while (x!=y) {
buf[++*buf] = d[x].y;
if (d[x].y==1) x = (x+P-1)%P;
else if (d[x].y==2) x = (x+1)%P;
else x = inv(x);
}
} int main() {
cin>>n>>m>>P;
bfs(n,d1),bfs(m,d2);
for (auto &&it:d1) {
if (d2.count(it.x)) {
int ans = it.y.x+d2[it.x].x;
if (ans>200) continue;
printf("%d\n", ans);
pr(it.x,n,d1);
PER(i,1,*buf) cout<<buf[i]<<' ';
pr(it.x,m,d2);
REP(i,1,*buf) {
if (buf[i]==1) buf[i]=2;
else if (buf[i]==2) buf[i]=1;
cout<<buf[i]<<' ';
}hr;
return 0;
}
}
}

Number Clicker CodeForces - 995E (中途相遇)的更多相关文章

  1. Number Clicker CodeForces - 995E(双向bfs)

    双向bfs  注意数很大  用map来存 然后各种难受....

  2. Prime Gift CodeForces - 912E (中途相遇)

    链接 大意:求素因子只含给定素数的第k大数 先二分答案转为判定x是第几大, 然后分两块合并即可, 按奇偶分块可以优化一下常数 #include <iostream> #include &l ...

  3. 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  4. Codeforces 995 E - Number Clicker

    E - Number Clicker 思路:双向搜索 代码: #include<bits/stdc++.h> using namespace std; #define fi first # ...

  5. uva 6757 Cup of Cowards(中途相遇法,貌似)

    uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 different characters (M ...

  6. LA 2965 Jurassic Remains (中途相遇法)

    Jurassic Remains Paleontologists in Siberia have recently found a number of fragments of Jurassic pe ...

  7. HDU 5936 Difference 【中途相遇法】(2016年中国大学生程序设计竞赛(杭州))

    Difference Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  8. 高效算法——J 中途相遇法,求和

    ---恢复内容开始--- J - 中途相遇法 Time Limit:9000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Su ...

  9. 【中途相遇+二进制】【NEERC 2003】Jurassic Remains

    例题25  侏罗纪(Jurassic Remains, NEERC 2003, LA 2965) 给定n个大写字母组成的字符串.选择尽量多的串,使得每个大写字母都能出现偶数次. [输入格式] 输入包含 ...

随机推荐

  1. linux常用命令:top 命令

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是 一个动态显示过程,即可以通过用户按键来不断刷 ...

  2. jboos下载地址记录

    JBoss在2006年被 RedHat 收购.在各种 J2EE 应用服务器中,JBoss 是最受欢迎而且功能最为强大的应用服务器.不过JBoss从8.0开始改名为WildFly,这个新名称在我看来似乎 ...

  3. DNS服务器原理介绍(一)

    DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串.通过主机名,最终 ...

  4. js监听页面是否在浏览器当前页面

    在最近的一个socket项目中,需要监听客户端是否已读客服端发送的消息. 这里用到了html5中document新增了一个事件 visibilitychange,这个事件在页面前台或后台切换时被触发, ...

  5. c++第十五天

    <c++ primer, 5E> 第94页到第99页,笔记: 1.迭代器(iterator):一种比下标访问更通用的访问容器中元素的机制. (并不是所有标准库容器都支持下标访问,<运 ...

  6. 作为从业人员,如果一定要学一门新的编程语言,那么它一定是c++

    作为从业人员,如果一定要学一门新的编程语言,那么它一定是c++. 无论各种编程语言排行榜如何变化,什么语言最赚钱,想成为真正的程序员,那么c/c++是必修课,因为几乎所有的底层都是c/c++编写的,各 ...

  7. jQuery 对象

    jQuery 对象 版权声明:未经博主授权,严禁转载分享 什么是 jQuery 对象 jQuery 对象是通过 jQuery 包装 DOM 对象后产生的对象. jQuery 对象是一个类数组对象. j ...

  8. FTP-Linux中ftp服务器搭建

    一.FTP工作原理 (1)FTP使用端口 [root@localhost ~]# cat /etc/services | grep ftp ftp-data 20/tcp #数据链路:端口20 ftp ...

  9. Python3基础 file for+文件指针 读取txt文本并 一行一行的输出(高效率)

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  10. 32位MD5加密补齐丢失的0

    /// <summary> /// 获取32位MD5加密字符串(已补完0) /// </summary> /// <param name="strWord&qu ...