AtCoder Beginner Contest 465 D - X to Y 题解

声明:

来看这条题解的看完你会觉得自己很聪明。

思路:

我们可以发现当 \(x\) 进行一次操作后会有以下两种值:

  1. \(\lfloor\frac{x}{K}\rfloor\)
  2. \(kx+t,t \in [0,k-1]\cap\N\)

你发现 \(kx+t\) 这个东西非常麻烦,究其根本就是这个万恶的 \(\lfloor\rfloor\),于是我们不考虑让 \(x\) 进行操作2,转而让 \(y\) 进行,于是就变成了:

  1. \(x=\lfloor\frac{x}{K}\rfloor\)
  2. \(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;
}
posted @ 2026-07-04 21:58  LZYXT  阅读(21)  评论(0)    收藏  举报