杭电hduoj 2504 又见GCD

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2504
算法特工队QQ羣:979618872 (伸手党绕边,欢迎有良好基础的人加入)

//
// Created by Leo Lee on 2019/4/5.
//

#include <iostream>
using namespace std;
long long gcd(long long  a,long long b);
int main(){
    int cases;
    cin>>cases;
    long long a,b;
    while(cin>>a>>b && cases--){
        for(int i = 2;i*b<=1000000;i++){
            long long res = gcd(a,i*b);
            if (b == res){
                cout<<i*b<<endl;
                break;
            }
        }
    }

    return 0;
}
long long gcd(long long  a,long long b){
    long long tmp;
    while(b!=0){
        tmp = a%b;
        a = b;
        b = tmp;
    }
    return a;
}
点赞