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 解题报告的更多相关文章

  1. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  2. USACO Section1.5 Number Triangles 解题报告

    numtri解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  3. LeetCode 476 Number Complement 解题报告

    题目要求 Given a positive integer, output its complement number. The complement strategy is to flip the ...

  4. Lintcode: Majority Number II 解题报告

    Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integ ...

  5. USACO Section 1.5 Number Triangles 解题报告

    题目 题目描述 现在有一个数字三角形,第一行有一个数字,第二行有两个数字,以此类推...,现在从第一行开始累加,每次在一个节点累加完之后,下一个节点必须是它的左下方的那个节点或者是右下方那个节点,一直 ...

  6. Winter-1-F Number Sequence 解题报告及测试数据

    Time Limit:1000MS     Memory Limit:32768KB Description ​A number sequence is defined as follows:f(1) ...

  7. CF995E Number Clicker (双向BFS)

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

  8. 【LeetCode】264. Ugly Number II 解题报告(Java & Python)

    标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...

  9. 【LeetCode】537. Complex Number Multiplication 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 日期 题目地址:https://leetcode.com/pr ...

随机推荐

  1. Linux文件系统与目录结构

    在Linux系统中,目录被组织成一个:单根倒置树结构,文件系统从根目录开始,用/来表示.文件名称区分大小写( 大小写敏感还需要看具体的文件系统格式 ),以.开头的为隐藏文件,路径用/来进行分割(win ...

  2. selenium破解极限

    一共分为两端段代码: 第一段:获取cookie 第二段:通过cookie登陆 这里以百度云为例: 第一部分:保存cookies,直接在cmd中执行就好 >>> from seleni ...

  3. linux学习(3)——vim文本编辑工具

    (三) vi与vim的最大区别就是编辑一个文本vi不显示颜色,vim显示颜色. 安装: yum install -y vim-enhance Vim有三种模式 A:一般模式 上下左右光标 k j h ...

  4. python爬虫-简单使用xpath下载图片

    首先 1.为方便以下进行 谷歌浏览器里要安装xpath脚本 2.下载一个lmxl     命令:pip install lxml 3. 以下三张图是一个,当时爬的 <糗事百科>里的图片 值 ...

  5. openwrt(二) 配置openwrt及编译

    导航 1. 配置openwrt 2. 编译openwrt 3. 错误记录 1. 配置openwrt 在openwrt的根目录下,执行make menuconfig. 这个界面我也只是了解了这两个选项而 ...

  6. [Bzoj3611]大工程(虚树+DP)

    Description 题目链接 Solution 在虚树上跑DP即可 关于虚树的建立,是维护一个最右链的过程 关键代码如下: sort(A+1,A+k+1,cmp);//按dfs序排序 s[top= ...

  7. scrapy如何实现分布式爬虫

    使用scrapy爬虫的时候,记录一下如何分布式爬虫问题: 关键在于多台主机协作的关键:共享爬虫队列 主机:维护爬取队列从机:负责数据抓取,数据处理,数据存储 队列如何维护:Redis队列Redis 非 ...

  8. Spring---配置文件概述

    概述 Spring 的配置文件是用于指导 Spring 工厂进行Bean的生产.依赖关系注入及 Bean 实例分发的“图纸”,它是一个或多个标准的XML文档,J2EE 程序员必须学会并灵活应用这份“图 ...

  9. BF算法(蛮力匹配算法)

    将主串M指定位置和目标串S开始位置进行对比,如果相同将M的下一个字符和S的下一个字符对比,如果不同则M的下一个字符和S的开始位置对比,直到S中每一个字符和M中的连续字符串相等,否则不匹配. C#代码- ...

  10. 顺序查找&二分查找&索引查找

    1.查找技术的分类.如下图: 2.什么是顺序查找呢?(无序表) 顺序查找的原理很简单,就是遍历整个列表,逐个进行记录的关键字与给定值比较,若某个记录的关键字和给定值相等,则查找成功,找到所查的记录.如 ...