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的更多相关文章
- LightOj 1215 - Finding LCM(求LCM(x, y)=L中的 y )
题目链接:http://lightoj.com/volume_showproblem.php?problem=1215 题意:已知三个数a b c 的最小公倍数是 L ,现在告诉你 a b L 求最 ...
- LightOj 1215 Finding LCM
Discription LCM is an abbreviation used for Least Common Multiple in Mathematics. We say LCM (a, b, ...
- LOJ Finding LCM(math)
1215 - Finding LCM Time Limit: 2 second(s) Memory Limit: 32 MB LCM is an abbreviation used for Least ...
- Finding LCM LightOJ - 1215 (水题)
这题和这题一样......只不过多了个数... Finding LCM LightOJ - 1215 https://www.cnblogs.com/WTSRUVF/p/9316412.html #i ...
- Finding LCM (最小公倍数)
Finding LCM Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu [Submit] ...
- lightoj 1215
lightoj 1215 Finding LCM 链接:http://www.lightoj.com/volume_showproblem.php?problem=1215 题意:已知 a, b, l ...
- 专题[vjudge] - 数论0.1
专题[vjudge] - 数论0.1 web-address : https://cn.vjudge.net/contest/176171 A - Mathematically Hard 题意就是定义 ...
- LightOj 1236 - Pairs Forming LCM (分解素因子,LCM )
题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意:给你一个数n,求有多少对(i, j)满足 LCM(i, j) = n, ...
- [POJ 2429] GCD & LCM Inverse
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10621 Accepted: ...
随机推荐
- Mysql连接出现时区问题
错误提示: The server time zone value '¥x¥_¼Ð·Ç®É¶¡' is unrecognized or represents more than one time zon ...
- idea 忽略不需要提交的文件
1.打开git bash界面,进入到某个项目的根目录,执行下面命令 touch .gitignore 此时,再该项目的根目录里,会创建.gitignore文本,打开该文本,编辑需要忽略的文件(编辑规则 ...
- 关于python列表的一些基础知识。
因学校实验室要求,自学了python,一开始看书觉得太简单了,也没有多动手去尝试,直到看完了前八章突然想动手试试的时候,人傻了,深刻体会到了好记性不如烂笔头的道理,故整理一些python列表的操作. ...
- restframework 分页组件、响应器
一.分页组件 1.PageNumberPagination a.全局配置 导入模块 from rest_framework.pagination import PageNumberPagination ...
- Scala 学习(6)之「对象」
目录 object 伴生对象 继承抽象类 apply方法 main方法 用 object 来实现枚举功能 object 相当于 class 的单个实例,通常在里面放一些静态的 field 或者 met ...
- X-CTF(REVERSE高级) Reversing-x64Elf-100
逻辑很简单,如果sub_4006FD函数返回假则返回Nice! 图1 进入sub_4006FD函数,加密过程也很简单,这里值得注意的有两点 一.8*(i%3)是二维数组的第一个参数,这里是取v3的地址 ...
- Linux学习笔记-Centos7搭建owncloud私有云盘
使用环境:虚拟机centos7 1.下载安装LAMP相关软件 [root@localhost yum.repos.d]# yum install httpd –y [root@localhost yu ...
- 文件系统(02):基于SpringBoot框架,管理Xml和CSV文件类型
本文源码:GitHub·点这里 || GitEE·点这里 一.文档类型简介 1.XML文档 XML是可扩展标记语言,是一种用于标记电子文件使其具有结构性的标记语言.标记指计算机所能理解的信息符号,通过 ...
- java工具类方法
1.生成16位数字(当时日期时间加随机两位数) public static String getNo16() { String getNo = getNo(); return getNo.substr ...
- tmobst6
1.(单选题)Oracle数据库中,在SQL语句中连接字符串的方法是:(). A)CAT B)CONCAT C)JOIN D)UNION 2.(单选题)在数据库中,有一个名为seq的序列对象,以下语句 ...