Recently Yaghoub is playing a new trick to sell some more. When somebody gives him A Tomans, he
who never has appropriate changes, asks for B Tomans such that lowest common multiple of A and B
equals to C and he will pay back a round bill. Or otherwise take some snack instead of the remaining of
his money. He believes that finding such a number is hard enough that dissuades students from paying
that.
You should write a program that help poor students giving the appropriate amount of money to
Yaghoub. Of course if there are several answers you go for students’ benefit which is the lowest of them.
Input
The first line begin with an integer T (T 100000), the number of tests. Each test that comes in a
separate line contains two integers A and C (1 A; C 107
).
Output
Print the lowest integer B such that LCM(A; B) = C in a single line. If no such integer exists, print
‘NO SOLUTION’ instead. (Quotes for clarity)
Sample Input
3
2 6
32 1760
7 16
Sample Output
3
55
who never has appropriate changes, asks for B Tomans such that lowest common multiple of A and B
equals to C and he will pay back a round bill. Or otherwise take some snack instead of the remaining of
his money. He believes that finding such a number is hard enough that dissuades students from paying
that.
You should write a program that help poor students giving the appropriate amount of money to
Yaghoub. Of course if there are several answers you go for students’ benefit which is the lowest of them.
Input
The first line begin with an integer T (T 100000), the number of tests. Each test that comes in a
separate line contains two integers A and C (1 A; C 107
).
Output
Print the lowest integer B such that LCM(A; B) = C in a single line. If no such integer exists, print
‘NO SOLUTION’ instead. (Quotes for clarity)
Sample Input
3
2 6
32 1760
7 16
Sample Output
3
55
NO SOLUTION
首先对于C不能整除A的状况肯定排除
然后得到B=C/A
然后取G=GCD(A,B)
如果G==1,那么此时B就是解
否则的话,就证明A,B,的最小公倍数肯定不是C,因为其最小公倍数是A*B/G
那么我们就去掉这个公因子,方法是A/G,B*G
循环直到G==1为止
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 1000005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
int main()
{
int t,a,b,c,r;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&a,&c);
if(c%a)
{
printf("NO SOLUTION\n");
continue;
}
b = c/a;
int g = gcd(a,b),k= 1;
while(g!=1)
{
k*=g;
a/=g;
g = gcd(a,b);
}
printf("%d\n",b*k);
}
return 0;
}