链接

大意: 给定模数$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;
}
}
}

最新文章

  1. Orchard中如何配置远端发布
  2. AndroidStudio中activity实现去掉标题栏
  3. PHP中文名文件下载实现
  4. JSP Servlet WEB生命周期
  5. 开始跟踪Redis啦,开帖
  6. 025医疗项目-模块二:药品目录的导入导出-HSSF导入类的封装
  7. javascript创建对象的方法总结
  8. Lantern免费使用教程【转】
  9. 嵌入式linux无线网卡的使用
  10. 非常实用的JQuery的选项卡切换源码
  11. 【转】System.Data.OracleClient requires Oracle client software version 8.1.7 or greater
  12. 如何将代码提交到git上
  13. 深入理解position属性&amp;containing block
  14. java的优点和误解 《java核心技术卷i》第一章
  15. ComM(通信管理)和CanNm(network)
  16. Spring Boot默认Initializer(1)——ConfigurationWarningsApplicationContextInitializer
  17. Struts2 Intercepter 笔记
  18. java--jvm启动的参数
  19. C#高级编程(第六版)学习:第三十一章:Windows窗体
  20. Git 自动补全

热门文章

  1. linux常用命令:wc 命令
  2. python2.7运行selenium webdriver api报错Unable to find a matching set of capabilities
  3. 2018-2019-2 20165209 《网络对抗技术》Exp1:PC平台逆向破解
  4. 俞敏洪:未来教育是互联网+ AI +区块链联合颠覆
  5. stm32 学习参考(转)
  6. Centos7.5 安装Netdata
  7. Quartz框架调用——运行报错:ThreadPool class not specified
  8. 20145105 《Java程序设计》实验二总结
  9. 小工具:使用Python自动生成MD风格链接
  10. Beetl模板引擎入门教程