Number Clicker CodeForces - 995E (中途相遇)
2024-08-31 03:56:44
大意: 给定模数$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;
}
}
}
最新文章
- Orchard中如何配置远端发布
- AndroidStudio中activity实现去掉标题栏
- PHP中文名文件下载实现
- JSP Servlet WEB生命周期
- 开始跟踪Redis啦,开帖
- 025医疗项目-模块二:药品目录的导入导出-HSSF导入类的封装
- javascript创建对象的方法总结
- Lantern免费使用教程【转】
- 嵌入式linux无线网卡的使用
- 非常实用的JQuery的选项卡切换源码
- 【转】System.Data.OracleClient requires Oracle client software version 8.1.7 or greater
- 如何将代码提交到git上
- 深入理解position属性&;containing block
- java的优点和误解 《java核心技术卷i》第一章
- ComM(通信管理)和CanNm(network)
- Spring Boot默认Initializer(1)——ConfigurationWarningsApplicationContextInitializer
- Struts2 Intercepter 笔记
- java--jvm启动的参数
- C#高级编程(第六版)学习:第三十一章:Windows窗体
- Git 自动补全
热门文章
- linux常用命令:wc 命令
- python2.7运行selenium webdriver api报错Unable to find a matching set of capabilities
- 2018-2019-2 20165209 《网络对抗技术》Exp1:PC平台逆向破解
- 俞敏洪:未来教育是互联网+ AI +区块链联合颠覆
- stm32 学习参考(转)
- Centos7.5 安装Netdata
- Quartz框架调用——运行报错:ThreadPool class not specified
- 20145105 《Java程序设计》实验二总结
- 小工具:使用Python自动生成MD风格链接
- Beetl模板引擎入门教程