1.又见GCD
有三个正整数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
解析:从c的2倍开始计算,直到找到一个b使得gcd(a,b) == c
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int t,a,b,c;
int gcd(int a,int b)
{
if(!b)
return a;
else
return gcd(b,a%b);
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&a,&b);
c = b*2;
while(gcd(a,c)!=b)
c += b;
printf("%d\n",c);
}
}
2.A/B
要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。
Input
数据的第一行是一个T,表示有T组数据。
每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。
Output
对应每组数据输出(A/B)%9973。
Sample Input
2 1000 53 87 123456789
Sample Output
7922 6060
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int b,n,t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&b);
int i;
for(i=0;i<9973;i++)
if((((b%9973)*i)%9973-n)%9973==0)
{
break;
}
printf("%d\n",i);
}
return 0;
}