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. Maven 工程读取resource下的文件

    1:方式1: public static List<String> userList; static { userList = new LinkedList<String>() ...

  2. FJOI2019游记

    应该是.....尽力了吧 day0:下午到酒店,晚上魔兽(with lyc)+雀魂.心里也清楚,今年只是来观光的. day1:上午暴力打完走人.下午和晚上魔兽+雀魂+300. day2:暴力打到一半拉 ...

  3. go 字符串反转(倒序)

    似乎没什么好办法,string的话也得需要先转换成rune再反转再转成string package main import ( "fmt" ) func reverseString ...

  4. 【题解】Luogu P4391 [BOI2009]Radio Transmission 无线传输

    原题传送门 这题需要用到kmp匹配 推导发现: 设循环节的长度为x,那么kmp数组前x个都是0,后面kmp[x+n]=n 先求出kmp数组 答案实际就是len-kmp[len] #include &l ...

  5. 2018-2019-1 20189206 《Linux内核原理与分析》第六周作业

    linux内核分析学习笔记 --第五章 系统调用的三层机制 学习重点--深入理解系统调用的过程 给MenuOS添加命令 添加命令的方式较为简单,在LinuxKernel/menu/test.c目录下, ...

  6. opencv学习之路(8)、基本图像运算——加减与或

    一.图像加法 #include<opencv2/opencv.hpp> #include<iostream> using namespace cv; using namespa ...

  7. kali linux 数据库分析工具简述

    bbqsql SQL盲注可能很难被利用. 当可用的工具工作时,它们运行良好,但是当它们不工作时,您必须编写自定义的东西. 这是耗时且乏味的. BBQSQL可以帮助你解决这些问题. BBQSQL是一个用 ...

  8. Codeforces Round #427 (Div. 2) Problem B The number on the board (Codeforces 835B) - 贪心

    Some natural number was written on the board. Its sum of digits was not less than k. But you were di ...

  9. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心

    Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...

  10. 【python017--函数对象1】

    一.函数 1.定义函数:def  函数名(): 2.调用函数:直接写函数的名称() >>> def MyFirstFunction():    print('this my firs ...