AtCoder Beginner Contest 465 D - X to Y 题解
声明:
来看这条题解的看完你会觉得自己很聪明。
思路:
我们可以发现当 \(x\) 进行一次操作后会有以下两种值:
- \(\lfloor\frac{x}{K}\rfloor\)
- \(kx+t,t \in [0,k-1]\cap\N\)
你发现 \(kx+t\) 这个东西非常麻烦,究其根本就是这个万恶的 \(\lfloor\rfloor\),于是我们不考虑让 \(x\) 进行操作2,转而让 \(y\) 进行,于是就变成了:
- \(x=\lfloor\frac{x}{K}\rfloor\)
- \(y=\lfloor\frac{y}{K}\rfloor\)
求使 \(x=y\) 最小操作次数即可。
代码:
#include<bits/stdc++.h>
using namespace std;
const int NUM=2e5+10;
#define int long long
int X,Y,K;
void work(){
cin>>X>>Y>>K;
int x=X,y=Y;
int ans=0;
while(x!=y){
if(x<y) swap(x,y);
x/=K,ans++;
}
cout<<ans<<'\n';
}
signed main(){
int t;
cin>>t;
while(t--)work();
return 0;
}

浙公网安备 33010602011771号