Test instructions: Give you two numbers, a,c, to find the minimum value of B at the time of the LCM (b) ==c
Idea: We know a property gcd (A, B) *LCM (A, b) = A*b
From this we can get B = gcd (A, A, a) *LCM (A, a)/a
Then we can first calculate the assumed B value with the LCM (A, a, a, a, or a)/a
If GCD (A.B) ==1 then the minimum value of B is determined
If GCD (A, B)!=1 we are going to find it by calculation
Calculation method is A=A/GCD (A, B) b=b*gcd (A.B)
Examples:
4
6 12
2 6
32 1760
7 16
Results: 4 3 NO Solution
#include <iostream>#definell Long Longusing namespacestd;intgcdintAintb) { if(b==0)returnA; Else returnGCD (b,a%b);}intMain () {intT; CIN>>T; while(t--) { intA,c; CIN>>a>>C; if(c%a!=0) {cout<<"NO Solution"<<Endl; Continue; } intans=c/A; intk=gcd (A,ans); while(k!=1) {a=a/K; Ans=ans*K; K=gcd (A,ans); } cout<<ans<<Endl; } return 0;}
Uva 11889 Benefit (LCM and GCD)