Ultraviolet A 11889-benefit (Math _ fast enumeration factor)

Source: Internet
Author: User

Recently yaghoub is playing a new trick to have some more. When somebody gives him ATomans, he who never has appropriate changes, asks BTomans such that lowest common multiple AAnd BEquals CAnd 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 shoshould 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( T100000), the number of tests. Each test that comes in a separate line contains two integers AAnd C(1 A, C107 ).

Output print the lowest integer BSuch that LCM( A, B) = CIn a single line. If no such integer exists, print" No solution"Instead. (quotes for clarity)

Sample Input

32 632 17607 16

Sample output

355NO SOLUTION
It is easy to give a and c The minimum integer B that satisfies lcm (a, B) = C. If no, "no solution" is output ".
LCM (a, B) = a * B/gcd (a, B) = c --> a * B = gcd (a, B) * C; --> A/gcd (a, B) = C/B, because a/gcd (a, B) must be an integer, so B must be a factor of C, enumerate the factors of C.
At first, the pure brute force enumeration of the factor T of C was triggered to understand that mathematics was king. The enumeration factor has been optimized when determining the prime number, that is, it only needs to be enumerated to SQRT (c ). Another condition is that a must be a factor of C. Because
b/gcd(a,b)==c/a; 
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <cctype>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>#include <list>#define ll long longusing namespace std;const int INF = 0x3f3f3f3f;ll gcd(ll a,ll b){if(b==0) return a;else return gcd(b,a%b);}void solve(ll a,ll c){// b/gcd(a,b)==c/aif(c%a){puts("NO SOLUTION");return ;}ll b=1,ans=INF;int m=floor(sqrt(c)+0.5);while(b<=m){if(c%b==0){if(a*b==c*gcd(a,b)){ans=min(ans,b);break;}ll sb=c/b;if(a*sb==c*gcd(a,sb))ans=min(ans,sb);}b++;}if(ans!=INF)printf("%lld\n",ans);elseputs("NO SOLUTION");}int main(){int t;ll a,b,c;scanf("%d",&t);//   a/gcd(a,b)==c/b;while(t--){scanf("%lld%lld",&a,&c);solve(a,c);}return 0;}


Ultraviolet A 11889-benefit (Math _ fast enumeration factor)

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.