跳到正文

题解

AT_abc465_d 题解

Moya_Rao约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年7月6日
收录
2026年7月11日
更新
2026年11月7日
篇幅
约 2 分钟
获赞
2

版本

  1. v1当前

这个 D 好难,比 E 难多了(确信。


我们发现两种操作是一对相反的操作,类似加法和减法的关系。或者更直白地说,如果 aa 通过其中一种操作变成了 bb,那 bb 也能通过另一种操作变回 aa。

然后你再考虑题目要求的从 XX 变成 YY,我们可以把任何变化的过程概括为,不断变小再不断变大的一个过程。

我们让 XX 和 YY 不断除以 kk(并向下取整),每次让两者中较大的去除,记录步数,直到 X′=Y′X' = Y' 停止。此时当前步数次一定能把 XX 变为 YY,因为先让 XX 除到 X′X' 的值,然后再不断乘上去直到变成 YY,恰好用了这么多步。

不难证明这个一定是最优的。于是就做完啦。

code && submission
C++
#include<bits/stdc++.h>
#define LL long long
#define UInt unsigned int
#define ULL unsigned long long
#define LD long double
#define pii pair<int,int>
#define pLL pair<LL,LL>
#define pDD pair<LD,LD>
#define fr first
#define se second
#define pb push_back
#define isr insert
#define _i128 __int128
using namespace std;
LL T,x,y,k,cnt;
LL read(){
    LL su=0,pp=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')pp=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){su=su*10+ch-'0';ch=getchar();}
    return su*pp;
}

int main(){
    T=read();
    while(T--){
        x=read(),y=read(),k=read();
        cnt=0;
        while(x!=y){
            if(x<y)swap(x,y);
            x/=k,cnt++;
        }
        cout<<cnt<<"\n";
    }
    return 0;
}

如果本篇题解对你有帮助的话,麻烦你点一个小小的赞,真是太感谢啦!

原文
存档评论4 条抓取于