Ultraviolet A 11889 benefit

Source: Internet
Author: User

(LCM (a, B) = C; C is the smallest common multiple of A and B. Now we have a and c. You need to find the smallest B.

Solution:
1. If C %! = 0 indicates no solution. Set B = C/A. When gcd (a, B) = 1, B indicates that B is the required result. If gcd (a, B )! = 1;
Then, lcm (a, B) must be smaller than C. Why do you think about this? Because some of the original results of a are the same as those of Result B?
A affects the value of B.
2. example: A = 12 = 2 ^ 2*3 ^ 1, B = 16 = 2 ^ 4, C = 48 = 2 ^ 4*3 ^ 1; B '= C/a = 4 = 2 ^ 2;
If B 'is the part of B that is different from a, then we obtain B' if gcd (A, B ')! = 1 indicates that a partially affects the result.
In this way, we need B '* gcd (A, B'), a/gcd (A, B '), and know that gcd (, B ') = 1.
Then B 'gets the original Result B.

// Excerpt from http://blog.sina.com.cn/s/blog_77dc9e080101jhq7.html

 

 

PS: the code is customized... Orz

# Include <iostream>
Using namespace STD;

Int A, C, B;
Int gcd (int A, int B ){
Return B = 0? A: gcd (B, A % B );
}

Int main (){
Int T;
Cin> T;
While (t --){
Cin> A> C;
If (C % A = 0 ){
B = C/;
Int D;
D = gcd (A, B );
While (D! = 1 ){
B * = D;
A/= D;
D = gcd (A, B );
}
Cout <B <Endl;
}
Else
Cout <"no solution" <Endl;
}
Return 0;
}

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.