杭电2504--又见GCD

又见GCD

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12877    Accepted Submission(s): 5504


Problem 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
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  2503 1215 2501 2566 1231 
天真的我竟然以为是2倍。 讨论区的神解释:
输入16 4,2倍b是8,它的最大公约数就变成8了,还符合题意吗?
所以16 4的正确答案应该是12.

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 int gcd(int a, int b)
 6 {
 7     if(b == 0)
 8         return a;
 9     else
10         return gcd(b, a % b); 
11 } 
12 int main()
13 {
14     int t;
15     scanf("%d", &t);
16     while(t--)
17     {
18         int n, m;
19         scanf("%d %d", &n, &m); 
20         int c = m * 2;
21         while(gcd(n, c) != m)
22             c += m;
23         printf("%d
", c);
24     }
25     return 0;    
26 } 
 
原文地址:https://www.cnblogs.com/soTired/p/4740393.html