LightOJ 1215 Finding LCM(数论)

题意  已知LCM(a, b, c) = L 和 a、b、L   求最小的满足等式的c.

把数展开为素因子积的形式后

GCD(a,b)就是a,b的公共素因子取在a、b中的较小指数

LCM(a,b)就是a,b的所有素因子取在a、b中的较大指数

令m = LCM(a,b)  那么问题转化为了求最小的c满足 LCM(m, c) = L

那么最小的c就是L中不在m中的素因子和L中指数大于m中指数的素因子取在L中的指数即积

#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为a,b的最大公约数

        if(l % m) puts("impossible");
        else
        {
            //要使lcm(c,m) = l  c中至少要有l中不在m中的素因子和l中指数大于m中的素因子取在l中的指数
            c = l / m;   //现在c中包含了l中不在m中的素因子取l中指数 和 l中指数大于m中的素因子取指数差
            //那么现在c还需要乘上c和m的公共素因子取m中的指数
            while((d = gcd(c, m)) != 1) //gcd(c,m) 取c,m公共素因子的小指数积
            {
                c = c * d, m = m / d;
                d = gcd(c, m);
            }
            printf("%lld\n", c);
        }
    }
    return 0;
}

1215 - Finding LCM

LCM is an abbreviation used for Least Common Multiple in Mathematics. We say LCM (a, b, c) = L if and only if L is the least integer which is divisible
by a, b and c.

You will be given a, b and L. You have to find c such that LCM (a, b, c) = L. If there are several solutions, print the one where c is as small as possible. If there is
no solution, report so.

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-29 19:39:08

LightOJ 1215 Finding LCM(数论)的相关文章

LightOJ - 1215 Finding LCM

Finding LCM 已知a, b, c的最小公倍数为L, 给你a,b,问你是否存在最小的c满足题意,不存在输出impossible 素数分解 1 #include <cmath> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define INF 0x3f3f3f3f 6 #define MOD 1000000007 7 using namespace std; 8

Finding LCM LightOJ - 1215 (水题)

这题和这题一样......只不过多了个数... Finding LCM LightOJ - 1215 https://www.cnblogs.com/WTSRUVF/p/9316412.html #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <set> #include <ve

HDU4497 GCD and LCM 数论 素数分解

题意很简单首先以前做最简单的LCM跟CGD的时候都知道先求出两个数A,B的最大公约数GCD,那么LCM可以利用  A*B/GCD来求得,这点一开始脑残了没想到,结果没有进行特盘所以错了,意思就是 题目给的L%G不为0的话就是无解,结果我给判其它的去了,肯定漏了些什么没有发现 然后对于 L/G进行素因子分解,同时任意的数都能够通过素因子分解来表示,所以三个解x,y,z也能分解 L/G = p1^q1*p2^q2.... x = p1^i1*... y = p1^j1*... z = p1^k1*.

Uva 11388 GCD LCM ( 数论 )

Uva  11388 GCD LCM( 数论 ) 题意: 求是否存在a,b 使得lcm(a,b) = L, gcd(a,b) = G,不存在输出-1,存在输出a,b,且a尽可能小 分析: 强行暴力是不可能的数据很大,要用llu,这里有两种思路 思路一: 由题意可知 a*b = G*L 保证a = G的倍数的情况下,枚举a再判断G*L能否整除a,最后判断b是否为a的倍数.a从G开始扫到sqrt(G*L) //输入两个整数G,L //找出a,b 使得 gcd(a,b) = G lcm(a,b) =

hdu 4497 GCD and LCM 数论 素数分解

GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1339    Accepted Submission(s): 607 Problem Description Given two positive integers G and L, could you tell me how many solutions of

UVA 10791 Minimum Sum LCM 数论

题目链接: https://vjudge.net/problem/UVA-10791 题目描述: 给一个数n, 让你求至少两个数的lcm是n 的, 最小和 解题思路: 唯一分解, 每个单独的素数的幂加起来就是答案 代码: #include <iostream> #include <cstdio> #include <string> #include <vector> #include <cstring> #include <iterator

LightOJ 1336 Sigma Function(数论 整数拆分推论)

--->题意:给一个函数的定义,F(n)代表n的所有约数之和,并且给出了整数拆分公式以及F(n)的计算方法,对于一个给出的N让我们求1 - N之间有多少个数满足F(x)为偶数的情况,输出这个数. --->分析:来考虑F(x)为奇数的情况,给据题目中给我们的公式,,如果F(x)为奇数,那么这个多项式里面的任何一项都必须是奇数,可以知道p = 2时,        p^e - 1肯定是奇数,如果p != 2,当且仅当e为偶数的时候,此项为奇数,证明如下: 原式变形为[ p^(e+1) -p + (

neu 1694 Primorial vs LCM 数论

1694: Primorial vs LCM 时间限制: 4 Sec  内存限制: 128 MB[提交][状态][讨论版] 题目描述 Given N (2<=N<=10^14), what is the quotient of LCM(1,2,3,....,N) divided by multiple of all primes up to N. As the result might be too big, output it's modulo by 1000000007. For exam

1694: Primorial vs LCM 数论

链接:戳这里 1694: Primorial vs LCM 时间限制: 4 Sec  内存限制: 128 MB [提交][状态][讨论版] 题目描述 Given N (2<=N<=10^14), what is the quotient of LCM(1,2,3,....,N) divided by multiple of all primes up to N. As the result might be too big, output it's modulo by 1000000007.