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: ...
随机推荐
- Jquery电子签名制作_jSignature
今天用Jquery的jSignature库制作一个电子签名 后台.net core上传到指定文件夹 下载jquery库 提取码:rd9g html @{ Layout = null; } <!D ...
- ORM 效率补充
1.only defer only: 获取数据表中某列或多列的值,注意获取的是对象,如果查询字段不是括号内的,效率反而变低 defer: 获取除了某列或某几列的数据,注意获取的是对象 User.ob ...
- xLua下使用lua-protobuf
本文发表于程序员刘宇的博客,转载请注明来源:https://www.cnblogs.com/xiaohutu/p/12168781.html protobuf作为一种通用套接字格式,各种插件里,最本质 ...
- elasticjob学习一:simplejob初识和springboot整合
Elastic-Job是一个分布式调度解决方案,由两个相互独立的子项目Elastic-Job-Lite和Elastic-Job-Cloud组成. Elastic-Job-Lite定位为轻量级无中心化解 ...
- java自定义注解学习(注解处理器)
如果没有用来读取注解的方法和工作,那么注解也就不会比注释更有用处了.使用注解的过程中,很重要的一部分就是创建于使用注解处理器.Java SE5扩展了反射机制的API,以帮助程序员快速的构造自定义注解处 ...
- sense8影评摘抄
“卡尔维诺在<为什么读经典>中<西诺拉在月球>一章里如是记述: 月球上的贵族光着身子四处走,仿佛这样还够,他们还在腰间悬挂阳具造型的铜饰.“我觉得这个习俗真是奇特.在我们的世界 ...
- oracle 11g数据库服务器安装
系统:windows7旗舰版 64位.oracle数据库服务器版本:oracle11g. 一.下载 1.登录oracle账户: 首先打开谷歌浏览器,输入网址[英文版网址:https://www.o ...
- Liunx(centos8)下的yum的基本用法和实例
yum 命令 Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的 ...
- 平滑重启更新(GR机制)
平滑重启更新(GR机制) 什么是平滑启动机制 是一种在协议重启时保证转发业务不中断的机制. 什么时候用到平滑重启 平滑重启一般应用于业务更新或者版本发布过程中,能够避免因为代码发布重启服务导致的暂时性 ...
- 关于selenium自动化元素定位问题解决的几种方法
遇到了元素定位问题和定位到后无法执行点击操作等,闲话少说,直奔主题: 1.元素定位不到一般有如下3种情况,大家如果遇到了可以对号入座哈 a.查找的元素不在当前窗口中 解决方法:使用driver.swi ...