UVa 11889 Benefit (number theory)

Source: Internet
Author: User
Tags gcd time limit

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,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.