题意:给出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)的更多相关文章

  1. CF995E Number Clicker (双向BFS)

    题目链接(洛谷) 题目大意 给定两个数 \(u\) , \(v\) .有三种操作: \(u=u+1(mod\) \(p)\) . \(u=u+p−1(mod\) \(p)\) . \(u=u^{p−2 ...

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

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

  3. Codeforces 995 E - Number Clicker

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

  4. HDU 3085 Nightmare Ⅱ (双向BFS)

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  5. Hdu1401-Solitaire(双向bfs)

    Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered ...

  6. UVA1601-The Morning after Halloween(双向BFS)

    Problem UVA1601-The Morning after Halloween Accept: 289 Submit: 3136 Time Limit: 12000 mSec  Problem ...

  7. 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 ...

  8. HDU3085(双向BFS+曼哈顿距离)题解

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. 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. ...

随机推荐

  1. 拖拽 支持ie6

    可随意拖拽方块至任一位置: 1.setCapture方法:多用于容器对象,效果是对指定的对象设置鼠标捕获.使在容器内的子对象的鼠标事件均由容器对象触发,因此,只能在容器对象的鼠标事件函数中进行处理.当 ...

  2. SQLServer2008设置开启远程连接

    SQL Server 2008默认是不允许远程连接的,sa帐户默认禁用的,如果想要在本地用SSMS连接远程服务器上的SQL Server 2008,需要做两个部分的配置:    1,SQL Serve ...

  3. 【BZOJ】2014: [Usaco2010 Feb]Chocolate Buying(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2014 这应该是显然的贪心吧,先排序,然后按花费取 #include <cstdio> # ...

  4. (转)负载均衡,回话保持,cookie

    servlet操作cookie:http://elf8848.iteye.com/blog/253198 负载均衡,回话保持:http://www.cnblogs.com/qq78292959/arc ...

  5. asp.net网站防恶意刷新的Cookies与Session解决方法

    本文实例讲述了asp.net网站防恶意刷新的Cookies与Session解决方法,是WEB程序设计中非常实用的技巧.分享给大家供大家参考.具体实现方法如下: Session版实现方法: public ...

  6. .NET程序调试技巧(一):快速定位异常的一些方法

    作为一个程序员,解BUG是我们工作中常做的工作,甚至可以说解决问题能力是一个人工作能力的重要体现.因为这体现了一个程序员的技术水平.技术深度.经验等等. 那么在我们解决BUG的过程中,定位问题是非常重 ...

  7. 基于chrome内核的.NET开发资源

    chrome Frame: 让IE有一颗chrome的心,看起来不错,但我没有深入研究这个东西. http://www.google.com/chromeframe?hl=zh-CN&quic ...

  8. Laravel5.1 分页展示

    Laravel为我们提供了一套分页的逻辑,我们无需自己实现分页逻辑,只需要执行几个简单的方法就能实现漂亮的分页. 1 simplePaginate 这是一种只显示上一页下一页的样式分页,我们来看看怎么 ...

  9. 学习tornado

    http://old.sebug.net/paper/books/tornado/ http://demo.pythoner.com/itt2zh/index.html http://tornado- ...

  10. mysql5.7的密码

    [root@mysql ~]# grep "temporary password" /var/log/mysqld.log 2018-04-03T08:08:05.867624Z ...