11889-benefit
Time limit:5.000 seconds
Http://uva.onlinejudge.org/index.php? OPTION=COM_ONLINEJUDGE&ITEMID=8&CATEGORY=467&PAGE=SHOW_PROBLEM&PROBLEM=29 89
Recently Yaghoub is playing a new trick to sell some. When somebody gives him A Tomans, Him who never has the appropriate, changes for B asks Tomans St Common multiple of a and B equals to C and he'll pay back A round bill. Or otherwise take some snack instead of the remaining of his. He believes that finding such a number are hard enough that dissuades students from paying that.
You are should write a program this help poor students giving the appropriate amount of. Of course if there are several answers your go for students ' benefit which is the lowest of them.
Input
The number of tests is the number of the 100000. Each test is comes in a separate line contains two integers a and C (1a, c 7).
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
1760
7 16
Sample Output
3
NO SOLUTION
Ideas:
We can use a and C's unique factor decomposition to find the smallest possible B-factor factorization, and then find B, but the complexity of this method is O (c).
Is there a quicker way?
Method 2:
Because
AB/GCD (a,b) =c,
Remember c/a as B ', then
B=b ' gcd (a,b),
Set
B=kb ',
Then there are
GCD (A,KB ') =k,