E - Number Clicker

思路:双向搜索

代码:

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<int,pii>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head int MOD;
int u, v, p;
map<int, pii>mp;
LL q_pow(LL n, LL k) {
LL ans = ;
while(k) {
if(k&) ans = (ans * n) % MOD;
n = (n*n) % MOD;
k >>= ;
}
return ans;
}
void print(int pos1, int pos2, int ty, int tt) {
vector<int>vc;
if(ty == ) {
int t = pos1;
while(t != u) {
if(mp[t].fi == ) {
vc.pb();
t = (t + MOD - ) % MOD;
}
else if(mp[t].fi == ) {
t = (t + ) % MOD;
vc.pb();
}
else if(mp[t].fi == ) {
t = q_pow(t, MOD - ) % MOD;
vc.pb();
}
}
reverse(vc.begin(), vc.end());
vc.pb(tt);
t = pos2;
while(t != v) {
if(mp[t].fi == ) {
vc.pb();
t = (t + MOD - ) % MOD;
}
else if(mp[t].fi == ) {
t = (t + ) % MOD;
vc.pb();
}
else if(mp[t].fi == ) {
t = q_pow(t, MOD - ) % MOD;
vc.pb();
}
}
}
else {
int t = pos2;
while(t != u) {
if(mp[t].fi == ) {
vc.pb();
t = (t + MOD - ) % MOD;
}
else if(mp[t].fi == ) {
t = (t + ) % MOD;
vc.pb();
}
else if(mp[t].fi == ) {
t = q_pow(t, MOD - ) % MOD;
vc.pb();
}
}
reverse(vc.begin(), vc.end());
if(tt != )vc.pb(-tt);
else vc.pb(tt);
t = pos1;
while(t != v) {
if(mp[t].fi == ) {
vc.pb();
t = (t + MOD - ) % MOD;
}
else if(mp[t].fi == ) {
t = (t + ) % MOD;
vc.pb();
}
else if(mp[t].fi == ) {
t = q_pow(t, MOD - ) % MOD;
vc.pb();
}
}
}
//cout << pos1 << " " << pos2 << " " << ty << endl;
printf("%d\n", (int)vc.size());
for (int i = ; i < vc.size(); i++) printf("%d ", vc[i]);
}
int main() {
scanf("%d %d %d", &u, &v, &p);
MOD = p;
queue<pii>q;
if(u == v) return *puts("");
q.push({u, });
q.push({v, });
mp[u] = {u, };
mp[v] = {v, };
while(!q.empty()) {
pii p = q.front();
q.pop();
int a = (p.fi + ) % MOD;
if(mp.find(a) != mp.end() && mp[a].se != p.se) {
print(p.fi, a, p.se, );
break;
}
else if(mp.find(a) == mp.end()){
mp[a] = {, p.se};
q.push({a, p.se});
}
a = (p.fi + MOD - ) % MOD;
if(mp.find(a) != mp.end() && mp[a].se != p.se) {
print(p.fi, a, p.se, );
break;
}
else if(mp.find(a) == mp.end()){
mp[a] = {, p.se};
q.push({a, p.se});
}
a = q_pow(p.fi, MOD - ) % MOD;
if(mp.find(a) != mp.end() && mp[a].se != p.se) {
print(p.fi, a, p.se, );
break;
}
else if(mp.find(a) == mp.end()){
mp[a] = {, p.se};
q.push({a, p.se});
}
}
return ;
}

Codeforces 995 E - Number Clicker的更多相关文章

  1. Codeforces 55D Beautiful Number

    Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...

  2. CF995E Number Clicker 解题报告

    CF995E Number Clicker 题目描述 Allen is playing Number Clicker on his phone. He starts with an integer u ...

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

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

  4. Number Clicker CodeForces - 995E (中途相遇)

    链接 大意: 给定模数$p$, 假设当前在$x$, 则可以走到$x+1$, $x+p-1$, $x^{p-2}$ (mod p), 求任意一条从u到v不超过200步的路径 官方题解给了两个做法, 一个 ...

  5. 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,并且给出每一步的操作. 分析:朴素的b ...

  6. Codeforces 40 E. Number Table

    题目链接:http://codeforces.com/problemset/problem/40/E 妙啊... 因为已经确定的格子数目严格小于了$max(n,m)$,所以至少有一行或者一列是空着的, ...

  7. Codeforces 124A - The number of positions

    题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't ...

  8. codeforces Soldier and Number Game(dp+素数筛选)

    D. Soldier and Number Game time limit per test3 seconds memory limit per test256 megabytes inputstan ...

  9. Codeforces 55D Beautiful Number (数位统计)

    把数位dp写成记忆化搜索的形式,方法很赞,代码量少了很多. 下面为转载内容:  a positive integer number is beautiful if and only if it is  ...

随机推荐

  1. Spring Boot 2 (八):Spring Boot 集成 Memcached

    Spring Boot 2 (八):Spring Boot 集成 Memcached 一.Memcached 介绍 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数 ...

  2. [VS] - "包含了重复的“Content”项。.NET SDK 默认情况下包括你项目中的“Content”项。

    copy to :http://www.cnblogs.com/jinzesudawei/p/7376916.html VS 2017 升级至  VS 2017 v15.3 后,.Net Core 1 ...

  3. 一次 Java 内存泄漏排查过程,涨姿势

    人人都会犯错,但一些错误是如此的荒谬,我想不通怎么会有人犯这种错误.更没想到的是,这种事竟发生在了我们身上.当然,这种东西只有事后才能发现真相.接下来,我将讲述一系列最近在我们一个应用上犯过的这种错误 ...

  4. linux command line send email

    https://www.tecmint.com/send-email-attachment-from-linux-commandline/ https://stackoverflow.com/ques ...

  5. PO VO BO DTO POJO DAO之间的关系

    J2EE开发中大量的专业缩略语很是让人迷惑,尤其是跟一些高手讨论问题的时候,三分钟就被人家满口的专业术语喷晕了,PO VO BO DTO POJO DAO,一大堆的就来了. PO:persistant ...

  6. 20145311王亦徐 《网络对抗技术》 MSF基础应用

    20145311王亦徐 <网络对抗技术> MSF基础应用 实验内容 掌握metasploit的基本应用方式以及常用的三种攻击方式的思路 主动攻击,即对系统的攻击,不需要被攻击方配合,以ms ...

  7. c++中ifstream一次读取整个文件

    转载:http://www.cnblogs.com/kex1n/p/4028428.html 第一种方法: 读取至std::string的情况: #include <string> #in ...

  8. lnmp部署知乎出现403

    查看错误日志: [root@web01 /]# tailf  /var/log/nginx/error.log 2019/01/16 19:02:06 [error] 10023#10023: *8 ...

  9. Linux电源管理_autosleep--(五)【转】

    本文转载自:https://blog.csdn.net/wlsfling/article/details/46005409 1. 前言 Autosleep也是从Android wakelocks补丁集 ...

  10. SCU 4439 Vertex Cover(二分图最小覆盖点)题解

    题意:每一条边至少有一个端点要涂颜色,问最少涂几个点 思路:最小顶点覆盖:用最少的点,让每条边都至少和其中一个点关联,显然是道裸最小顶点覆盖题: 参考:二分图 代码: #include<iost ...