//hdu_2.1.5 被阴了一招,已知a,b,求最小的c满足gcd(a,c)=b,b!=c;一下子想到不就是2*b吗
//没想到就不一定是2b啊。。。不如a=6b,gcd(6b,2b)明显是2*b而不是b啊,具体a=12,b=; c=14才可以哪
#include<iostream>
using namespace std;
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
int main()
{
int a,b,c,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&a,&b);
c=2*b;
while(gcd(a,c)!=b)
{
c+=b;
}
printf("%d\n",c);
}
return 0;
}
又见GCD
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 660 Accepted Submission(s): 365
//hdu_2.1.5 被阴了一招,已知a,b,求最小的c满足gcd(a,c)=b,b!=c;一下子想到不就是2*b吗
//没想到就不一定是2b啊。。。不如a=6b,gcd(6b,2b)明显是2*b而不是b啊,具体a=12,b=; c=14才可以哪
#include<iostream>
using namespace std;
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
int main()
{
int a,b,c,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&a,&b);
c=2*b;
while(gcd(a,c)!=b)
{
c+=b;
}
printf("%d\n",c);
}
return 0;
}