Codeforces 995 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的更多相关文章
- Codeforces 55D Beautiful Number
Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...
- CF995E Number Clicker 解题报告
CF995E Number Clicker 题目描述 Allen is playing Number Clicker on his phone. He starts with an integer u ...
- Number Clicker CodeForces - 995E(双向bfs)
双向bfs 注意数很大 用map来存 然后各种难受....
- Number Clicker CodeForces - 995E (中途相遇)
链接 大意: 给定模数$p$, 假设当前在$x$, 则可以走到$x+1$, $x+p-1$, $x^{p-2}$ (mod p), 求任意一条从u到v不超过200步的路径 官方题解给了两个做法, 一个 ...
- 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 ...
- Codeforces 40 E. Number Table
题目链接:http://codeforces.com/problemset/problem/40/E 妙啊... 因为已经确定的格子数目严格小于了$max(n,m)$,所以至少有一行或者一列是空着的, ...
- Codeforces 124A - The number of positions
题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't ...
- codeforces Soldier and Number Game(dp+素数筛选)
D. Soldier and Number Game time limit per test3 seconds memory limit per test256 megabytes inputstan ...
- Codeforces 55D Beautiful Number (数位统计)
把数位dp写成记忆化搜索的形式,方法很赞,代码量少了很多. 下面为转载内容: a positive integer number is beautiful if and only if it is ...
随机推荐
- P2473 [SCOI2008]奖励关(期望)
P2473 [SCOI2008]奖励关 $n<=15$,显然的状压 设$f[i][w]$表示前$i$轮,状态$w$的最大期望 蓝后我们发现一个问题:$f[i][w]$可能是非法的 于是我们从$f ...
- 深入理解 Vue 组件
深入理解 Vue 组件 组件使用中的细节点 使用 is 属性,解决组件使用中的bug问题 <!DOCTYPE html> <html lang="en"> ...
- oracle RAC如何正确地删除ASM磁盘组
1.登录到命令行 切换到grid用户 [grid@swnode1 ~]$ sqlplus / as sysasm SQL*Plus: Release Production on Wed May :: ...
- 自动发现实现url+响应时间监控
url自动发现脚本: [root@jenkins scripts]# cat urlDiscovery.py #!/usr/bin/env python #coding:utf-8 import o ...
- Codeforces Round #479 (Div. 3)题解
CF首次推出div3给我这种辣鸡做,当然得写份博客纪念下 A. Wrong Subtraction time limit per test 1 second memory limit per test ...
- 【python37--面向对象】
一. self是什么 绑定方法,self就是实例对象的唯一标志 >>> class Ball: def setName(self,name): self.name = name de ...
- java开发注意点之String使用equals和==的区别
"=="操作符的作用 1.用于基本数据类型的比较 2.判断引用是否指向堆内存的同一块地址. equals所在位置: 在Object类当中,而Object是所有类的父类,包含在jdk ...
- 流程控制if,while,for
if语句 什么是if语句 判断一个条件如果成立则做...不成立则做....为何要有if语句 让计算机能够像人一样具有判断的能力 如何用if语句 语法1: if 条件1: code1 code2 cod ...
- bzoj2086: [Poi2010]Blocks DP,单调栈
题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2086 思路 这就有点妙了 题目意思就是让你求平均数>=k的最长序列 先求出a[i] ...
- p1654 OSU!
期望DP 设\(g[i]\)表示前i个的连续1的期望长度,\(h[i]\)表示前i个连续1的长度的平方的期望,\(f[i]\)表示前i个的期望得分 由期望的线性性质,我们可以考虑统计新增一个对答案的贡 ...