您的位置:首页 > 其它

hdu 2504 又见gcd(gcd)

2015-03-25 08:33 274 查看

又见GCD

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 12117 Accepted Submission(s): 5146



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
《ACM程序设计》短学期考试_软件工程及其他专业

题目大意:求取他t=a/b的值,然后找到与t互质的最小的数即可

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;

int n,a,b;

int gcd ( int a , int b )
{
    return b?gcd(b,a%b):a;
}

int main ( )
{
    while ( ~scanf ( "%d" , &n ) )
    {
        while ( n-- )
        {
            scanf ( "%d%d" , &a , &b );
            int t = a/b;
            if ( b == 1 ) printf ( "%d\n" , a == 2 ? 3:2 );
            else
            {
                int i = 2;
                while ( i*b < 100001 )
                {
                   if ( gcd ( i , t ) == 1 ) break;
                   else i++;
                }
                printf ( "%d\n" , i*b );
            } 
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: