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 ...
随机推荐
- P4381 [IOI2008]Island(基环树+单调队列优化dp)
P4381 [IOI2008]Island 题意:求图中所有基环树的直径和 我们对每棵基环树分别计算答案. 首先我们先bfs找环(dfs易爆栈) 蓝后我们处理直径 直径不在环上,就在环上某点的子树上 ...
- maven单元测试报java.lang.IllegalStateException: Failed to load ApplicationContext
报这个异常java.lang.IllegalStateException: Failed to load ApplicationContext的时候,通常是因为applicationContent.x ...
- Linux 系统下安装 python-skimage
Linux 系统下安装 python-skimage 安装必须的依赖 // python-mumpy // python-scipy // python-matplotlib $ sudo apt-g ...
- Fedora 全局代理上网设置
1.首先新建一个bash文件 vim /etc/profile.d/proxy.sh 加入以下内容 (有账号密码) export HTTP_PROXY="http://username:pa ...
- Computer Study
2017.12.15日开始制定长期学习计划: 大二上: 目前短期计划: 1.每天一道PAT乙级(弄完PAT乙级)+校OJ(打基础). 2.先复习C++,17周后开始复习数据结构.(补任务 刚刚发 ...
- Python3 tkinter基础 Frame bind 鼠标移动事件 实时显示鼠标的位置
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- bzoj2152: 聪聪可可 点分治
链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2152 luogu爆搜都能过,总时间超过100ms就是写错了 思路 直接mod上面跑点分治就行 ...
- Minikube 安装
安装Minikube 在容器编排工具中安装配置最复杂的就是Kubernetes,想要运行一个简单的容器集群环境,对于没有使用过Kubernetes的人来说,需要花费一定的时间去理解各组件的概念和功能, ...
- 论文笔记:Diffusion-Convolutional Neural Networks (传播-卷积神经网络)
Diffusion-Convolutional Neural Networks (传播-卷积神经网络)2018-04-09 21:59:02 1. Abstract: 我们提出传播-卷积神经网络(DC ...
- 【.NET开发之美】使用ComponentOne提高.NET DataMap中的加载速度
概述 FlexGrid for WinForm 采用了最新的数据绑定技术,并与Microsoft .NET Framework无缝集成. 因此,您可以获得易于使用的灵活网格控件,用于创建用户友好界面, ...