Lightoj 1215 Finding LCM (number theory)

Source: Internet
Author: User
Tags greatest common divisor



Test instructions The LCM (a, b, c) = L and A, B, l are known to be the least satisfying equation C.



After the number is expanded into the form of a factor product



GCD (A, A, b) is the lesser exponent of the common factor



The LCM (A, A, a, b) is the larger exponent of all the element factors in a



Make M = LCM (A, B) the problem translates in order to find the smallest C-satisfied LCM (M, c) = L



So the smallest c is the element factor in L that is not in M and L medium exponent is greater than m, and the exponent in L is the product




#include <bits/stdc++.h>
Using namespace std;
Typedef long 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%lld%lld", &a, &b, &l);
        m = a * b / gcd(a, b); //m is the greatest common divisor of a, b

        If(l % m) puts("impossible");
        Else
        {
            / / To make lcm (c, m) = l c at least have a prime factor in l not in m and l index in the index greater than m in the index in l
            c = l / m; // Now c contains the prime factor in l that is not in m, and the exponent in l is greater than the prime factor in m.
            // Then c now needs to multiply the public prime factor of c and m to take the index in m
            While((d = gcd(c, m)) != 1) //gcd(c,m) takes a small exponential product of c,m common prime factors
            {
                c = c * d, m = m / d;
                d = gcd(c, m);
            }
            Printf("%lld\n", c);
        }
    }
    Return 0;
} 




1215-finding LCM


LCM is a abbreviation used for LEast Common Multiple in mathematics. We say LCM (A, b, c) = L if and only if L are the least integer which is divisible by a, B and C7>c.



You'll be given a, B and L. You have the to find C such. LCM (A, b, c) = L. If There is several solutions, print the one where C is as small as possible. If there is no solution.


Input


Input starts with an integer T (≤325), denoting the number of test cases.



Each case starts with a line containing three integers a b L (1≤a, b≤106, 1≤l≤1012).


Output


For each case, print the case number and the minimum possible value of C. 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 NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.



Lightoj 1215 Finding LCM (number theory)


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.