链接

大意: 给定模数$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服务器配置---ftp用户黑名单

    用户黑白名单 一个Linux主机中会多个用户,而我们希望有些用户不能去访问ftp.ftp服务器可以通过配置文件“/etc/vsftpd/user_list”来设置一个用户列表,这个列表可以是黑名单,也 ...

  2. Ubuntu系统下在github中新增库的方法

    上一篇介绍了Ubuntu16.04系统下安装git的方法.本博客介绍怎么在github上怎么新建库. 如图 root@ranxf:/home/ranxf/learnGit/ranran_jiekou# ...

  3. Socket和ServletSocket的交互

    ServerSocket(int port) 是服务端绑定port端口,调accept()监听等待客户端连接,它返回一个连接队列中的一个socket. Socket(InetAddress addre ...

  4. 2D 2 3D 开源项目

    http://www.cvlibs.net/projects.php http://www.cvlibs.net/software/libelas/

  5. centos/rhel 6.5(更新至centos 7)下rabbitmq安装(最简单方便的方式)

    vim /etc/hosts 增加 127.0.0.1 hostname 不然启动的时候可能会报如下错误: [root@devel2 rabbitmq]# rabbitmq-server ERROR: ...

  6. stm32时钟树讲解

    1.管理好时钟,功耗才能更低

  7. VC++开机自动启动程序的几种方法 (转载)

    转载:http://blog.csdn.net/zhazhiqiang/article/details/51909703 很多监控软件要求软件能够在系统重新启动后不用用户去点击图标启动项目,而是直接能 ...

  8. c/c++ 动态申请数组(转载)

    转载:http://blog.csdn.net/hondely/article/details/6779887 转载:http://bbs.csdn.net/topics/390721031 转载:h ...

  9. Python数据分析入门之pandas基础总结

    Pandas--"大熊猫"基础 Series Series: pandas的长枪(数据表中的一列或一行,观测向量,一维数组...) Series1 = pd.Series(np.r ...

  10. IntelliJ-IDEA和Git、GitHub、Gitlab的使用

    一.基本入门 1.IntelliJ-IDEA预装的版本控制介绍 我们来看IntelliJ-IDEA的版本控制设置区域 打开File>Settings>Version Control  可以 ...