LightOJ 1215 Finding LCM (number theory), lightojlcm
It is known that LCM (a, B, c) = L and c.
After expanding the number into the form of prime factor Product
GCD (a, B) is a small index of the common prime factor of a and B.
LCM (a, B) is a larger index of all prime factors of a and B.
So that m = LCM (a, B) then the problem is converted to finding the minimum c to meet LCM (m, c) = L
Then the smallest c is the prime factor in L which is not in m and the exponent in L is greater than that in m. The prime factor in L is the product of the exponent in L.
# Include <bits/stdc ++. h> using namespace std; typedef long ll; ll gcd (ll a, ll B) {return B? Gcd (B, a % B): a ;}int main () {int T; ll a, B, l, c, d, m; scanf ("% d ", & T); for (int cas = 1; cas <= T; ++ cas) {printf ("Case % d:", cas ); scanf ("% lld", & a, & B, & l); m = a * B/gcd (a, B); // m is, maximum common divisor of B if (l % m) puts ("impossible"); else {// to enable lcm (c, m) = l c should contain at least the element l is not in m and the index in l is greater than the element in m. Take the index c = l/m in l; // now c contains the prime factor in l that is not in m. Take the index in l and the index in l that is greater than the prime factor in m to obtain the exponential difference. // now c needs to multiply the prime factor in c and take the exponent while (d = gcd (c , M ))! = 1) // gcd (c, m) returns the small exponential product of the common prime factors of c and m {c = c * d, m = m/d; d = gcd (c, m) ;}printf ("% lld \ n", c) ;}} return 0 ;}
1215-Finding LCM
LCMIs an abbreviation usedLEastCOmmonMUltiple in Mathematics. We sayLCM (a, B, c) = LIf and only ifLIs the least integer which is divisibleA, BAndC.
You will be givenA, BAndL. You have to findCSuch thatLCM (a, B, c) = L. If there are several solutions, print the one whereCIs as small as possible. If there is no solution, report so.
Input
Input starts with an integerT (≤ 325), Denoting the number of test cases.
Each case starts with a line containing three integersA B L (1 ≤ a, B ≤ 106, 1 ≤ L ≤ 1012).
Output
For each case, print the case number and the minimum possible valueC. If no solution is found, print'Impossible'.
Sample Input |
Output for Sample Input |
3 3 5 30 209475 6992 77086800 2 6 10 |
Case 1: 2 Case 2: 1 Case 3: impossible |
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.