CF995E Number Clicker 解题报告
CF995E Number Clicker
题目描述
Allen is playing Number Clicker on his phone.
He starts with an integer u u on the screen. Every second, he can press one of 3 buttons.
Turn \(u \to u+1 \pmod{p}\).
Turn \(u \to u+p-1 \pmod{p}\).
Turn \(u \to u^{p-2} \pmod{p}\).
Allen wants to press at most 200 buttons and end up with v v on the screen. Help him!
输入输出格式
输入格式:
The first line of the input contains 3 positive integers: \(u, v, p\)( \(0 \le u, v \le p-1\) , \(3 \le p \le 10^9 + 9\) ). \(p\) is guaranteed to be prime.
输出格式:
On the first line, print a single integer \(\ell\) , the number of button presses.
On the second line, print integers \(c_1, \dots, c_\ell\), the button presses.
For \(1 \le i \le \ell\) , \(1 \le c_i \le 3\).
We can show that the answer always exists.
长见识了。
发现这道题可以建成一个比较随机的图。
根据生日攻击,我们基本上只需要图的点数\(p\)开根号个\(\sqrt p\),就可以找到答案了。
于是进行双向搜索,合并答案即可。
Code:
#include <cstdio>
#include <map>
#define ll long long
const int N=2e5;
ll u,v,p,q[N+10],l=1,r=0,s[N],tot;
std::map <ll,ll > pre,used,opt,pre0,opt0;
void in(ll now,ll to,ll op)
{
if(!used[to])
{
used[to]=1;
opt[to]=op;
pre[to]=now;
q[++r]=to;
}
}
ll quickpow(ll d,ll k)
{
ll f=1;
while(k)
{
if(k&1) f=f*d%p;
d=d*d%p;
k>>=1;
}
return f;
}
int bfs0()
{
q[++r]=u;
while(l<=r&&r<=N)
{
ll now=q[l++];
if(now==v)
{
while(now!=u) s[++tot]=opt[now],now=pre[now];
printf("%lld\n",tot);
for(int i=tot;i;i--)
printf("%lld ",s[i]);
return 1;
}
ll to=(now+1)%p;
in(now,to,1);
to=(now+p-1)%p;
in(now,to,2);
to=quickpow(now,p-2);
in(now,to,3);
}
return 0;
}
void in0(ll now,ll to,ll op)
{
if(!used[to])
{
used[to]=1;
opt0[to]=op;
pre0[to]=now;
q[++r]=to;
}
}
ll tmp;
void swap(ll &x,ll &y){tmp=x,x=y,y=tmp;}
void bfs1()
{
used.clear();
l=1,r=0;
q[++r]=v;
while(l<=r&&r<=N)
{
ll now=q[l++];
if(pre.find(now)!=pre.end())
{
ll t=now;
while(now!=u) s[++tot]=opt[now],now=pre[now];
for(int i=1;i<=tot>>1;i++) swap(s[i],s[tot+1-i]);
now=t;
while(now!=v) s[++tot]=opt0[now],now=pre0[now];
printf("%lld\n",tot);
for(int i=1;i<=tot;i++)
printf("%lld ",s[i]);
return;
}
ll to=(now+1)%p;
in0(now,to,2);
to=(now+p-1)%p;
in0(now,to,1);
to=quickpow(now,p-2);
in0(now,to,3);
}
}
int main()
{
//freopen("dew.out","w",stdout);
scanf("%lld%lld%lld",&u,&v,&p);
if(bfs0()) return 0;
bfs1();
return 0;
}
2018.10.9
CF995E Number Clicker 解题报告的更多相关文章
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- USACO Section1.5 Number Triangles 解题报告
numtri解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- LeetCode 476 Number Complement 解题报告
题目要求 Given a positive integer, output its complement number. The complement strategy is to flip the ...
- Lintcode: Majority Number II 解题报告
Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...
- USACO Section 1.5 Number Triangles 解题报告
题目 题目描述 现在有一个数字三角形,第一行有一个数字,第二行有两个数字,以此类推...,现在从第一行开始累加,每次在一个节点累加完之后,下一个节点必须是它的左下方的那个节点或者是右下方那个节点,一直 ...
- Winter-1-F Number Sequence 解题报告及测试数据
Time Limit:1000MS Memory Limit:32768KB Description A number sequence is defined as follows:f(1) ...
- CF995E Number Clicker (双向BFS)
题目链接(洛谷) 题目大意 给定两个数 \(u\) , \(v\) .有三种操作: \(u=u+1(mod\) \(p)\) . \(u=u+p−1(mod\) \(p)\) . \(u=u^{p−2 ...
- 【LeetCode】264. Ugly Number II 解题报告(Java & Python)
标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...
- 【LeetCode】537. Complex Number Multiplication 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://leetcode.com/pr ...
随机推荐
- Java分享笔记:File类中常用方法的介绍
java.io包下的File类用于描述和创建一个文件或文件夹对象,只能对文件或文件夹做一些简单操作,不能修改文件的内容,功能比较有限.下面是对于File类中常用方法的程序演示. [1] 演示程序一 p ...
- python基础数据类型之字符串操作
1.字符串切片ps:字符串是不可变的对象, 所以任何操作对原字符 是不会有任何影响的 s1 = "python最简洁" print(s1[0]) print(s1[1]) prin ...
- 【转载】char*,const char*和string 三者转换
本文转自 http://blog.csdn.net/perfumekristy/article/details/7027678 const char* 和string 转换 const char*转换 ...
- 【PHP】Maximum execution time of 30 seconds exceeded解决办法
Maximum execution time of 30 seconds exceeded,今天把这个错误的解决方案总结一下: 简单总结一下解决办法: 报错一:内存超限,具体报错语句忘了,简单说一下解 ...
- Optimization Tipss for Multi Vendor eCommerce Software to drive, retain more sales
1. Make the Registration & Listing simple - Only if you keep the registration process and produ ...
- Django项目发布到Apache2.4配置mod_wsgi,解决遭遇的各种坑。
环境: Apache2.4 32bit Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Inte ...
- Codeforces Round 97B 点分治
B. Superset time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- 20145202马超GDB调试汇编堆栈过程分析
20145202马超GDB调试汇编堆栈过程分析 esc :w保存,:wq保存并退出 x:删除错误的单个字母 dw:删除整个单词 gcc hello.c -o hello:运行hello.c gcc - ...
- 关于 package.json 和 package-lock.json 文件说明
package.json 在 Node.js 中,模块是一个库或框架,也是一个 Node.js 项目.Node.js 项目遵循模块化的架构,当我们创建了一个 Node.js 项目,意味着创建了一个模块 ...
- Spring_依赖注入思想
Ioc(Inversion of Control) 控制反转,DI(Dependncy Injection)依赖注入,其实是指同一种思想.举例说明: 张三在某公司负责供水问题.有两种形式,第一种老板指 ...