题意:给出u,v,p,对u可以进行三种变化: 1.u=(u+1)%p ; 2.u = (u+p-1)%p;  3.u = 模p下的逆元。问通过几步可以使u变成v,并且给出每一步的操作。

分析:朴素的bfs或dfs会超时或炸栈,考虑用双向bfs头尾同时搜。用map存每个数的访问状态和对应的操作编号,正向搜步长为正,反向搜步长为负。反向搜的时候要注意对应加减操作是反过来的。

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<queue>
  6. #include<map>
  7. #include<string>
  8. #include<stack>
  9. using namespace std;
  10. typedef long long LL;
  11. const int maxn =;
  12. const int INF=0x3f3f3f3f;
  13. struct Node{
  14. int step, but;
  15. LL pre;
  16. };
  17. map<LL,Node> path;
  18. LL u,v,p;
  19.  
  20. LL fpow(LL a,LL n)
  21. {
  22. LL res=;
  23. while(n){
  24. if(n&) res =(res*a)%p;
  25. a = a*a %p;
  26. n>>=;
  27. }
  28. return res;
  29. }
  30.  
  31. void Print(LL a,LL b,int op)
  32. {
  33. printf("%d\n",path[a].step--path[b].step);
  34. stack<int> S;
  35. while(a!=u){
  36. Node ap = path[a];
  37. S.push(ap.but);
  38. a = ap.pre;
  39. }
  40. while(!S.empty()){
  41. int x =S.top();S.pop();
  42. printf("%d ",x);
  43. }
  44. printf("%d ",op);
  45. while(b!=v){
  46. Node bp =path[b];
  47. printf("%d ",bp.but);
  48. b = bp.pre;
  49. }
  50. puts("");
  51. }
  52.  
  53. void BFS()
  54. {
  55. path.clear();
  56. path[u]=(Node){,-,-};
  57. path[v]=(Node){-,-,-};
  58. queue<LL> qf,qb;
  59. qf.push(u); qb.push(v);
  60. while(!qf.empty()|| !qb.empty()){
  61. if(!qf.empty()){
  62. LL x = qf.front(); qf.pop();
  63. Node xp = path[x];
  64. LL next = (x+)%p;
  65. Node np = path[next];
  66. if(np.step==){ //未访问
  67. path[next]= (Node){xp.step+,,x};
  68. qf.push(next);
  69. }
  70. else if(np.step<){ //相遇
  71. Print(x,next,);
  72. return;
  73. }
  74.  
  75. next = (x+p-) %p; //op2
  76. np = path[next];
  77. if(np.step==){
  78. path[next]= (Node){xp.step+,,x};
  79. qf.push(next);
  80. }
  81. else if(np.step<){
  82. Print(x,next,);
  83. return;
  84. }
  85.  
  86. next = fpow(x,p-);
  87. np = path[next];
  88. if(np.step==){
  89. path[next] = (Node){xp.step+,,x};
  90. qf.push(next);
  91. }
  92. else if(np.step<){
  93. Print(x,next,);
  94. return;
  95. }
  96. }
  97.  
  98. if(!qb.empty()){
  99. LL x = qb.front(); qb.pop();
  100. Node xp = path[x];
  101.  
  102. LL next = (x+p-)%p;
  103. Node np = path[next];
  104. if(!np.step){
  105. path[next] = (Node){xp.step-,,x};
  106. qb.push(next);
  107. }
  108. else if(np.step>){
  109. Print(next,x,);
  110. return;
  111. }
  112.  
  113. next = (x+)%p;
  114. np = path[next];
  115. if(!np.step){
  116. path[next] = (Node){xp.step-,,x};
  117. qb.push(next);
  118. }
  119. else if(np.step>){
  120. Print(next,x,);
  121. return;
  122. }
  123.  
  124. next = fpow(x,p-);
  125. np = path[next];
  126. if(!np.step){
  127. path[next] = (Node){xp.step-,,x};
  128. qb.push(next);
  129. }
  130. else if(np.step>){
  131. Print(next,x,);
  132. return;
  133. }
  134. }
  135. }
  136. }
  137.  
  138. int main()
  139. {
  140. #ifndef ONLINE_JUDGE
  141. freopen("in.txt","r",stdin);
  142. freopen("out.txt","w",stdout);
  143. #endif
  144. int N,M,tmp,T;
  145. while(scanf("%lld %lld %lld",&u,&v,&p)==){
  146. if(u==v){
  147. puts("");
  148. continue;
  149. }
  150. BFS();
  151. }
  152. return ;
  153. }

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. Switch选择语句能否作用在String【字符串】上,也就是能否这么写:Switch(一个字符串变量)?

    Switch选择语句能否作用在String[字符串]上,也就是能否这么写:Switch(一个字符串变量)? 解答:不可以,只能处理int,byte,short,char,(其实是只能处理int,其它三 ...

  2. Android开发人员必须掌握的10 个开发工具+应该深入学习的10个开源应用项目

    一.Android开发人员必须掌握的10 个开发工具 Android SDK 本身包含很多帮助开发人员设计.开发.测试和发布 Android 应用的工具,在本文中,我们将讨论 10 个最常用的工具. ...

  3. 将Centos的yum源更换为国内的阿里云(163)源

    阿里云是最近新出的一个镜像源.得益于阿里云的高速发展,这么大的需求,肯定会推出自己的镜像源.阿里云Linux安装镜像源地址:http://mirrors.aliyun.com/ CentOS系统更换软 ...

  4. java锁和同步

    Java 语言设计中的一大创新就是:第一个把跨平台线程模型和锁模型应用到语言中去,Java 语言包括了跨线程的关键字synchronized 和 volatile,使用关键字和java类库就能够简单的 ...

  5. VS2008 调试出现错误 "Unable to start debugging."

    之前用的好好的调试功能,今天“F5”出现了 "Unable to start debugging." 的错误: 解决办法: 打开工程属性,选择“Debugging”,看看“Debu ...

  6. python3----练习题(过滑块验证)

    # 导入模块 from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webd ...

  7. MYSQL批量插入数据库实现语句性能分析【转】 批量插入!程序里面对于数据库插入的功能尽量用【异步处理+批量插入+(事务)】

    假定我们的表结构如下 代码如下   CREATE TABLE example (example_id INT NOT NULL,name VARCHAR( 50 ) NOT NULL,value VA ...

  8. UI层复习笔记

    在main文件中,UIApplicationMain函数一共做了三件事 根据第三个参数创建了一个应用程序对象 默认写nil,即创建的是UIApplication类型的对象,此对象看成是整个应用程序的一 ...

  9. iOS tabbar 属性

    1.设置tabbar背景颜色 NSArray *controllers = [NSArray arrayWithObjects:nav_main,nav_channle,nav_me, nil]; _ ...

  10. RPC远程过程调用概念及实现

    RPC框架学习笔记 >>什么是RPC RPC 的全称是 Remote Procedure Call 是一种进程间通信方式. 它允许程序调用另一个地址空间(通常是共享网络的另一台机器上)的过 ...