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. access数据库多个left join示例

    代码: /// <summary> /// 分类检索 查询selectname /// </summary> public static DataTable GetSelect ...

  2. sql server索引功能资料

    无论何时对基础数据执行插入.更新或删除操作,SQL Server 数据库引擎都会自动维护索引.随着时间的推移,这些修改可能会导致索引中的信息分散在数据库中(含有碎片).当索引包含的页中的逻辑排序(基于 ...

  3. .net开发微信公众平台

    一.说明:公众平台信息接口为开发者提供了一种新的消息处理方式,只有申请成为开发者后,你才能使用公众平台的开发功能,在这里你需要填写一个URL和一个Token,这两项信息也需要你拥有自己的服务器(外网服 ...

  4. 重新想象 Windows 8 Store Apps (69) - 其它: 自定义启动屏幕, 程序的运行位置, 保持屏幕的点亮状态, MessageDialog, PopupMenu

    [源码下载] 重新想象 Windows 8 Store Apps (69) - 其它: 自定义启动屏幕, 程序的运行位置, 保持屏幕的点亮状态, MessageDialog, PopupMenu 作者 ...

  5. 一些java的书籍

    netty in action 中文版:http://pan.baidu.com/s/1pLnEKZL spring security-3.0.1:http://pan.baidu.com/s/1bp ...

  6. mybatis中自建的类型别名

    在使用mybatis过程中经常用到类型别名,除了我们自己新建的别名外,mybatis还自带了很多类型别名和java中的类型的映射,下面先看一个自建的别名的配置 <typeAliases> ...

  7. 那一夜,我们..奋笔疾书敲出的--->>库存管理系统

    说了会再见,最近好吗?无论你在哪里>也许你在温暖的家,或许你在身在异乡的城市;或许你高高的峰顶放生高歌,或许你还在陡峭的山峰半空努力攀爬.......相信我们都会登上顶峰,"会当凌绝顶 ...

  8. windows下启动mongodb

    http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/#mongodb-as-a-windows-service C:\ ...

  9. 我在用的mac软件(1)--终端环境之iTerm2

    之前一直有朋友要我分享下在用的mac软件,今天有空就来写一下,可能不止于软件,会有一些配置或者工具,或者叫环境更合适.有些可能一句话带过,有些会详细介绍.也不分类了,想到哪个就写出来.如果已经写的足够 ...

  10. JAVA基础学习day27--反射机制

    一.概述 1.1.概述 反射的概念: 在Java中的反射机制是指在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法; 对于任意一个对象,都能够调用它的任意一个方法; 这种动态获取信息以及动 ...