hdu-又见GCD
又见GCD
Description
有三个正整数a,b,c(0<a,b,c<10^6),其中c不等于b。若a和c的最大公约数为b,现已知a和b,求满足条件的最小的c。
Input
第一行输入一个n,表示有n组测试数据,接下来的n行,每行输入两个正整数a,b。
Output
输出对应的c,每组测试数据占一行。
Sample Input
2
6 2
12 4
Sample Output
4
8
1 #include<stdio.h> 2 3 int gcd(int m, int n) 4 { 5 int t; 6 if (m < n) 7 { 8 t = m; 9 m = n; 10 n = t; 11 } 12 while (m % n != 0) 13 { 14 t = m % n; 15 m = n; 16 n = t; 17 } 18 return n; 19 } 20 21 int main() 22 { 23 int t,i; 24 int a, b, x; 25 scanf("%d", &t); 26 while (t--) 27 { 28 scanf("%d%d",&a,&b); 29 x = a / b; 30 for (i = 2; ;i++) 31 { 32 if ((i * b != b) && (gcd(x,i) == 1)) 33 { 34 printf("%d\n", i * b); 35 break; 36 } 37 } 38 } 39 return 0; 40 }