题解:P11275
题面
![[../../题面/洛谷题面/P11275|P11275]]
思路
考虑数学,从 \(u\) 到 \(v\) 有以下几种情况。
\(u=v\) , \(ans=0\)
\(\text{max(u,v)}\) 是 \(\text{min(u,v)}\) 的倍数, \(ans=\text{max(u,v)}\)
否则考虑从该点到 \(1\) 之外任意一个点中转再到 \(v\) 点,一定不如 \(u \rightarrow 1 \rightarrow u\) 优,此时 \(ans=u+v\),可以发现从 \(u\) 到 \(1\) 之外任意数花费大于等于 \(u\),再从该点到 \(v\) 点的花费大于等于 \(v\),最终 \(ans=u+v\)。
代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+1;
typedef long long ll;
ll q,x,y;
int main(){
cin>>q;
while(q--){
cin>>x>>y;
if(x==y)cout<<0<<endl;//情况一
else if(x%y==0||y%x==0)cout<<max(x,y)<<endl;//情况二
else cout<<x+y<<endl;//情况三
}
return 0;
}

浙公网安备 33010602011771号