又见GCD(hdu2504)

/*
微软招聘信息(主要针对已经有工作经验的)
 
又见GCD
Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6407    Accepted Submission(s): 2634


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
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 1000000
using namespace std;
bool hash[N];
int LCM(int n,int m)
{
    int temp;
    while(m%n!=0)
    {
        temp=m%n;
        m=n;
        n=temp;
    }
    return n;
}
void myth()
{
    int i,j=0;
    for(j=4;j<N;j+=2)
        hash[j]=1;    
    for(i=3;i<1000;i+=2)    
        if(!hash[i])    
            for(j=i*i;j<N;j+=i)    
                hash[j]=1; 
}
int main()
{
    int i,T,ans,m,n;
    myth();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&m,&n);
        if(m==n)
        {
            printf("%d\n",m);
            continue;
        }
        ans=m/n;
        for(i=2;i<N;i++)
            if(!hash[i]&&LCM(ans,i)==1)
                break;
            printf("%d\n",i*n);        
    }
    return 0;
}
原文地址:https://www.cnblogs.com/heqinghui/p/2785995.html