1215 - Finding LCM

Time Limit: 2 second(s)
Memory Limit: 32 MB

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

3

3 5 30

209475 6992 77086800

2 6 10

Output for Sample Input

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;

代码:

   1: #include <iostream>

   2: #include <algorithm>

   3: #include <cstring>

   4: using namespace std;

   5: typedef long long ll;

   6:  

   7: ll gcd(ll a,ll b){

   8:     if(a<b) swap(a,b);

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

  10: }

  11:  

  12: ll lcm(ll a,ll b){

  13:     return a/gcd(a,b)*b;

  14: }

  15:  

  16: int run()

  17: {

  18:     ll a,b,cas=1,L,T;

  19:     cin>>T;

  20:     while(T--)

  21:     {

  22:         cin>>a>>b>>L;

  23:         ll m=lcm(a,b);

  24:         if(m>L||L%m!=0)

  25:         {

  26:             cout<<"Case "<<cas++<<": "<<"impossible"<<endl;

  27:             continue;

  28:         }

  29:         ll c=L/m,g;

  30:         if(c!=1)

  31:           while((g=gcd(m,c))!=1){

  32:               c*=g,m/=g;

  33:           }

  34:         cout<<"Case "<<cas++<<": "<<c<<endl;

  35:     }

  36:     return 0;

  37: }

  38:  

  39: int main()

  40: {

  41:     ios::sync_with_stdio(0);

  42:     return run();

  43: }

LOJ Finding LCM(math)的更多相关文章

  1. Finding LCM (最小公倍数)

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

  2. Finding LCM LightOJ - 1215 (水题)

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

  3. 1215 - Finding LCM

    1215 - Finding LCM   LCM is an abbreviation used for Least Common Multiple in Mathematics. We say LC ...

  4. LOJ 6229 LCM / GCD (杜教筛+Moebius)

    链接: https://loj.ac/problem/6229 题意: \[F(n)=\sum_{i=1}^n\sum_{j=1}^i\frac{\mathrm{lcm}(i,j)}{\mathrm{ ...

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

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

  6. LightOj 1215 Finding LCM

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

  7. 【原创】开源Math.NET基础数学类库使用(09)相关数论函数使用

                   本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新  开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...

  8. 开源Math.NET基础数学类库使用(09)相关数论函数使用

    原文:[原创]开源Math.NET基础数学类库使用(09)相关数论函数使用               本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4 ...

  9. 专题[vjudge] - 数论0.1

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

随机推荐

  1. 【循序渐进学Python】10.模块和包

    1.导入模块 任何Python程序都可以作为模块导入,只要Python解释器能找到我们定义的模块所在位置即可,一般来讲,在一个模块被导入时,Python解释器会按照下面的步骤进行搜索: 在当前所在目录 ...

  2. Android Fragment 基本介绍

    Fragment 源码:http://www.jinhusns.com/Products/Download/?type=xcj Android是在Android 3.0 (API level 11)开 ...

  3. JavaScript常用代码

       页面的按钮全选:   function CheckBoxAll() {             //得到页面上所有input表单元素:document.getElementsByTagName( ...

  4. JMS学习(四) Selector详解

    一.前言 在掌握了消息的结构之后,我们接下来看一下JMS的一个重要功能:选择器.有些时候,作为消费者只希望处理自己感兴趣的消息.如果某个消息只有一个消费者,我们可以在让该客户端根据规则来处理自己感兴趣 ...

  5. (旧)子数涵数·PS——换脸

    一.打开Photoshop,打开下载好的两张素材图(均在百度下载的). 二.选中人物脸的部分,不需要很精确(哪种抠图方式都行,我用的是矩形选框工具),再使用移动工具(快捷键为V,或者按着Ctrl),将 ...

  6. 【GOF23设计模式】适配器模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_适配器模式.对象适配器.类适配器.开发中场景  适配器模式  笔记本电脑只有USB接口,新买的键盘是PS2接口的,需要用适 ...

  7. 怎样让js循环重复执行过程

    setInterval(function(){ cc();},60000);setInterval是每隔一分钟就执行一次方法体,主要特点是循环不断的执行.而setTimeout是执行一次就不会继续执行 ...

  8. JS之跨域

    今天学了跨域,迫不及待想跟大家分享!不妥之处希望大家指正. 首先来明确一下"跨域"这个概念. 跨域指的是,到外域去取数据.那什么是"外域"呢?我们先来了解同域. ...

  9. python基础之常用模块以及格式化输出

    模块简介 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要 ...

  10. 今天发现新大陆:haml和Emmet

    其实一开始小渣渣我只是想接触一下(css预处理器)sass,可是突然冒出一个haml. 原文是酱紫的. Sass 是采用 Ruby 语言编写的一款 CSS 预处理语言,它诞生于2007年,是最大的成熟 ...