CodeForces - 995E Number Clicker (双向BFS)
题意:给出u,v,p,对u可以进行三种变化: 1.u=(u+1)%p ; 2.u = (u+p-1)%p; 3.u = 模p下的逆元。问通过几步可以使u变成v,并且给出每一步的操作。
分析:朴素的bfs或dfs会超时或炸栈,考虑用双向bfs头尾同时搜。用map存每个数的访问状态和对应的操作编号,正向搜步长为正,反向搜步长为负。反向搜的时候要注意对应加减操作是反过来的。
#include<stdio.h>
#include<iostream>
#include<cstring>
#include<iostream>
#include<queue>
#include<map>
#include<string>
#include<stack>
using namespace std;
typedef long long LL;
const int maxn =;
const int INF=0x3f3f3f3f;
struct Node{
int step, but;
LL pre;
};
map<LL,Node> path;
LL u,v,p; LL fpow(LL a,LL n)
{
LL res=;
while(n){
if(n&) res =(res*a)%p;
a = a*a %p;
n>>=;
}
return res;
} void Print(LL a,LL b,int op)
{
printf("%d\n",path[a].step--path[b].step);
stack<int> S;
while(a!=u){
Node ap = path[a];
S.push(ap.but);
a = ap.pre;
}
while(!S.empty()){
int x =S.top();S.pop();
printf("%d ",x);
}
printf("%d ",op);
while(b!=v){
Node bp =path[b];
printf("%d ",bp.but);
b = bp.pre;
}
puts("");
} void BFS()
{
path.clear();
path[u]=(Node){,-,-};
path[v]=(Node){-,-,-};
queue<LL> qf,qb;
qf.push(u); qb.push(v);
while(!qf.empty()|| !qb.empty()){
if(!qf.empty()){
LL x = qf.front(); qf.pop();
Node xp = path[x];
LL next = (x+)%p;
Node np = path[next];
if(np.step==){ //未访问
path[next]= (Node){xp.step+,,x};
qf.push(next);
}
else if(np.step<){ //相遇
Print(x,next,);
return;
} next = (x+p-) %p; //op2
np = path[next];
if(np.step==){
path[next]= (Node){xp.step+,,x};
qf.push(next);
}
else if(np.step<){
Print(x,next,);
return;
} next = fpow(x,p-);
np = path[next];
if(np.step==){
path[next] = (Node){xp.step+,,x};
qf.push(next);
}
else if(np.step<){
Print(x,next,);
return;
}
} if(!qb.empty()){
LL x = qb.front(); qb.pop();
Node xp = path[x]; LL next = (x+p-)%p;
Node np = path[next];
if(!np.step){
path[next] = (Node){xp.step-,,x};
qb.push(next);
}
else if(np.step>){
Print(next,x,);
return;
} next = (x+)%p;
np = path[next];
if(!np.step){
path[next] = (Node){xp.step-,,x};
qb.push(next);
}
else if(np.step>){
Print(next,x,);
return;
} next = fpow(x,p-);
np = path[next];
if(!np.step){
path[next] = (Node){xp.step-,,x};
qb.push(next);
}
else if(np.step>){
Print(next,x,);
return;
}
}
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int N,M,tmp,T;
while(scanf("%lld %lld %lld",&u,&v,&p)==){
if(u==v){
puts("");
continue;
}
BFS();
}
return ;
}
CodeForces - 995E Number Clicker (双向BFS)的更多相关文章
- CF995E Number Clicker (双向BFS)
题目链接(洛谷) 题目大意 给定两个数 \(u\) , \(v\) .有三种操作: \(u=u+1(mod\) \(p)\) . \(u=u+p−1(mod\) \(p)\) . \(u=u^{p−2 ...
- Number Clicker CodeForces - 995E(双向bfs)
双向bfs 注意数很大 用map来存 然后各种难受....
- Codeforces 995 E - Number Clicker
E - Number Clicker 思路:双向搜索 代码: #include<bits/stdc++.h> using namespace std; #define fi first # ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- Hdu1401-Solitaire(双向bfs)
Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered ...
- UVA1601-The Morning after Halloween(双向BFS)
Problem UVA1601-The Morning after Halloween Accept: 289 Submit: 3136 Time Limit: 12000 mSec Problem ...
- Eight (HDU - 1043|POJ - 1077)(A* | 双向bfs+康拓展开)
The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...
- HDU3085(双向BFS+曼哈顿距离)题解
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- POJ-3131-Cubic Eight-Puzzle(双向BFS+哈希)
Description Let's play a puzzle using eight cubes placed on a 3 × 3 board leaving one empty square. ...
随机推荐
- MPAndroidChart -- LimitLine的坑
好久没有写博客了,最近比较忙,项目中使用了MPAndroidChart,初次使用也比较蠢,很多API都得一个一个查,但有些时候当你想应该有这个API的时候他却没有,就比如今天的主角LimitLine ...
- IDEA中如何配置Tomcat和项目?
IDEA是我用的挺多的一款java代码编辑工具,对于刚接触这款软件的新手来说,配置项目是很麻烦的了,更别说配置服务器Tomcat了,那么通过我的教程大家一定觉得配置IDEA项目也是很轻松的事了. ...
- 什么是 Web?
Web这个词刚开始显得有些泛泛,似乎“冲浪”.“网上存在”以及“主页”等等都和它拉上了一些关系. 甚至还有一种“Internet 综合症”的说法,对许多人狂热的上网行为提出了质疑.我们在这里有必要作一 ...
- 【BZOJ】1632: [Usaco2007 Feb]Lilypad Pond(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1632 我简直是个sb... ... bfs都不会写.. 算方案还用2个bfs! 都不会整合到一个! ...
- html input accept类型
在上传文件的时候,需要限制指定的文件类型. <input type="file" accept="image/*" /> accept表示可以上传文 ...
- java基础-集合笔记
Iterator(foreach) 遍历时只能通过iterator去删除(添加)元素,不能直接通过集合对象删除或添加元素 Set HashSet底层是一个HashMap HashSet添加元素,先判断 ...
- shell基础(二)
echo命令 Shell 的 echo 指令是用于字符串的输出. #!/bin/sh read name #读取标准输入的行 echo "$name It is a test" e ...
- js高级面试题
<script> //1. var foo = { bar: function () { return this.baz; }, baz: 1 }; (function () { retu ...
- npm安装express 公司上网设置代理
异常: 0 info it worked if it ends with ok1 verbose cli [ 'C:\\Program Files (x86)\\nodejs\\\\node.exe' ...
- 【BZOJ1576】[Usaco2009 Jan]安全路经Travel 最短路+并查集
[BZOJ1576][Usaco2009 Jan]安全路经Travel Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, ...