比赛链接:CodeForces Good Bye 2023
B题目传送门:B.Two Divisors
题目:
分析:
分析答案发现,x与a,b最小公倍数相关。
但某些情况下,x比a,b最小公倍数小,即a,b不互质时。
当a,b互质,x是a,b的最小公倍数。
代码:
import java.util.Scanner;
public class Main {
public static long gcd(long x,long y) {
if(y==0) return x;
else return gcd(y,x%y);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0) {
long a = sc.nextLong();
long b = sc.nextLong();
long c = gcd(a,b);
long ans = a*b/c;
if(b%a==0) ans *=(b/a);
System.out.println(ans);
}
}
}