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

题意:lcm(a,b,c)=L;现已知a,b,L的值,求是否存在c满足lcm(a,b,c)=L。

::首先求出a,b的最小公倍数m,则c必包含因子t=L/m;

令g=gcd(c,m);

假设c=t,c*m/g=L,当且仅当gcd(c,m)=1等式成立;

如果gcd(c,m)>1;

那么令(c*g)*(m/g)/gcd(c*g,m/g)=L;当且仅当gcd(c*g,m/g)=1;

如果gcd(c*g,m/g)>1重复上述操作;

例:a=2,b=3,L=12;

则m=6,L=12,t=2;

令c=t;判断gcd(6,2)==2,令c=c*2(==4),m=m/2(==3)

gcd(c,m)==1,故c=4;

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>

using namespace std;
typedef long long ll;

ll gcd(ll a, ll b)
{
if(a < b)
swap(a,b);

return b == 0 ? a : gcd(b, a%b);

}
ll lcm(ll a, ll b)
{
return a / gcd(a, b) * b;
}
int main()
{
int T, cas;
ll a, b, l;
scanf("%d", &T);

cas = 0;

while(T--)
{
cas++;

scanf("%lld%lld%lld", &a, &b, &l);

ll m = lcm(a, b);

ll c = l / m;

if(m > l || l % m != 0)
{
printf("Case %d: impossible\n",cas);
continue;
}

ll g = gcd(c, m);
if(c != 1)
{
while(g != 1)
{
c *= g;
m /= g;
g = gcd(c, m);

}

}

printf("Case %d: %lld\n", cas, c);

}
return 0;
}

1215 - Finding LCM的更多相关文章

  1. LightOj 1215 - Finding LCM(求LCM(x, y)=L中的 y )

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1215 题意:已知三个数a b c 的最小公倍数是 L ,现在告诉你 a b  L 求最 ...

  2. LightOj 1215 Finding LCM

    Discription LCM is an abbreviation used for Least Common Multiple in Mathematics. We say LCM (a, b, ...

  3. LOJ Finding LCM(math)

    1215 - Finding LCM Time Limit: 2 second(s) Memory Limit: 32 MB LCM is an abbreviation used for Least ...

  4. Finding LCM LightOJ - 1215 (水题)

    这题和这题一样......只不过多了个数... Finding LCM LightOJ - 1215 https://www.cnblogs.com/WTSRUVF/p/9316412.html #i ...

  5. Finding LCM (最小公倍数)

    Finding LCM Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu [Submit]   ...

  6. lightoj 1215

    lightoj 1215 Finding LCM 链接:http://www.lightoj.com/volume_showproblem.php?problem=1215 题意:已知 a, b, l ...

  7. 专题[vjudge] - 数论0.1

    专题[vjudge] - 数论0.1 web-address : https://cn.vjudge.net/contest/176171 A - Mathematically Hard 题意就是定义 ...

  8. LightOj 1236 - Pairs Forming LCM (分解素因子,LCM )

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意:给你一个数n,求有多少对(i,  j)满足 LCM(i, j) = n, ...

  9. [POJ 2429] GCD & LCM Inverse

    GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10621   Accepted: ...

随机推荐

  1. 【5min+】传说中的孪生兄弟? Memory and Span

    系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...

  2. CCPC-wannafly Camp Day2 讲课内容总结(杜瑜皓-数据结构)

    ·栈.单调栈 1.栈的特点与基本操作 2.单调栈 单调栈是一种特殊的栈,其栈内的元素都保持一个单调性(单调递增或者递减). ·单调递增栈,从栈底到栈顶依次递增(单调非递减栈:允许有相等) ·单调递减栈 ...

  3. ContractPattern 面向面向契约模式

  4. dotnet restore 初次运行 这个 指令 会安装 特别多的 4.0.0 或者 4.1 的 rc2-24027的 东东 这些东西。

  5. JavaScript中this的使用及含义(总结)

    this 1.谁调用,指向谁 2.事件监听函数中,this指向,绑定监听函数的那一个元素节点 即,谁绑,指向谁 3.当一个函数没有被其他对象调用时,(普通调用),this指向全局对象(严格模式下面是u ...

  6. CSS-11-外边距

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. Django中model的class Meta

    Class Meta 作用:使用内部类来提供一些metadata,以下列举一些常用的meta:1,abstract:如下段代码所示,将abstract设置为True后,CommonInfo无法作为一个 ...

  8. AOP编程实践总结

    AOP编程实践总结 AOP概述 AOP(Aspect-Oriented Programming,面向方面编程)是OOP(Object-Oriented Programing,面向对象编程)的补充和完善 ...

  9. TensorFlow 编程基础

    1.TensorFlow 安装:https://www.cnblogs.com/pam-sh/p/12239387.html https://www.cnblogs.com/pam-sh/p/1224 ...

  10. (5千字)由浅入深讲解动态规划(JS版)-钢条切割,最大公共子序列,最短编辑距离

    斐波拉契数列 首先我们来看看斐波拉契数列,这是一个大家都很熟悉的数列: // f = [1, 1, 2, 3, 5, 8] f(1) = 1; f(2) = 1; f(n) = f(n-1) + f( ...