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. Python3 Pandas的DataFrame格式数据写入excle文件、json、html、剪贴板、数据库

    Python3 Pandas的DataFrame格式数据写入excle文件.json.html.剪贴板.数据库 一.DataFrame格式数据 Pandas是Python下一个开源数据分析的库,它提供 ...

  2. 14: element ui 使用

    1.1 element ui 基本使用 参考网址: http://element.eleme.io/#/zh-CN/component/button 1.初始一个vue项目并安装element ui ...

  3. Kali系列之Hydra/Medusa mysql密码爆破

    hydra hydra -L /home/chenglee/zidian/user.txt -P /home/chenglee/zidian/wordlist.TXT 192.168.137.133 ...

  4. Magnum Kubernetes源码分析(二)

    Kubernetes Master Stack kubernetes master的stack的resources主要分为三个部分. master wait handle wait handle主要用 ...

  5. 【python51--__name__属性】

    一.基础知识 1.__name__ == '__main__' 所有模块都有一个__name__属性,__name__的值取决于如何应用模块,在作为独立程序运行的时候,__name__属性的值是‘__ ...

  6. topcoder srm 680 div1

    problem1 link 将限制按照$x$排序.那么$[upTo_{i}+1,upTo_{i+1}]$中数字个数为$quantity_{i+1}-quantity_{i}$.然后进行动态规划.$f[ ...

  7. Apache下的ArrayUtils工具类总结

    ArrayUtils中的方法: 1.add():将给定的数据添加到指定的数组中,返回一个新的数组. 2.addAll():合并两个数组. 3.contains():检查该数据在该数组中是否存在,返回一 ...

  8. What is event bubbling and capturing?

    What is event bubbling and capturing? 答案1 Event bubbling and capturing are two ways of event propaga ...

  9. s2-045漏洞批量检测工具

    今天晚上看老铁们在群里就这个st2-045漏洞讨论得火热,个人不太喜欢日站,本来想直接写个批量挂马的东西,但是想想还是算了,如果你有兴趣,改改也很容易,反正不关我的事 测试图 2017-3-8更新 增 ...

  10. AtCoder Beginner Contest 117 解题报告

    果然abc都是手速场. 倒序开的qwq. D题因为忘记1e12二进制几位上界爆了一发. A - Entrance Examination 就是除一下就行了... 看样例猜题意系列. #include& ...