杭电2504又见gcd

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2504

考点:基本的gcd运算和数学逻辑运算;

思路:

b是a,c的最大公约数,因此c可以被b整除,即c是b的倍数。
通过循环,求a和i*b的公约数等于b的时候;

这时i*b就是所要求的c

难度:一般,但是考察数学推导

Talk is cheap. Show me the code.

#include<bits/stdc++.h>//hduoj 2504
using namespace std;
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int a,b,c;
        cin>>a>>b;
        int tmp=a/b;
        for(register int i=2;;i++)//从第一个素数开始
        {
            if(__gcd(a,i*b)==b)//求当二者的公约数是b的时候,就满足题意了
            {
                int c=i*b;
                printf("%d\n",c);
                break;
            }
        }
    }
    return 0;
}

 

posted @ 2022-05-04 14:48  江上舟摇  阅读(5)  评论(0编辑  收藏  举报