LightOj 1215 Finding LCM
Discription
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
Sample Output
Case 1: 2
Case 2: 1
Case 3: impossible
本来感觉这个题太水了就不想写博客了2333,但是考虑到我马上要做的那个题可能要用到这个题 一个东西(但是回头再看这句话时突然打脸),所以还是写一下。
可以先求出lcm(a,b)=p,然后本质就是求一个 最小的 X 使得 lcm(X,p) = L。
无解很好判,只要p不是L的约数就无解。
考虑到lcm是对指数取max,而我们的目的是让X最小,所以我们可以让X在p和L次数相同的质因子上的次数取0,而在其他的质因子上取L在这上面的次数。
所以我们可以直接对 L/p 质因子分解, 然后 这里的质因子就是所有X要和L次数一样的质因子,只要把p和L/p在这上面的指数加起来就好啦。
#include<bits/stdc++.h>
#define ll unsigned long long
using namespace std;
int T,d[100],num=0;
ll a,b,L,ans=1; ll gcd(ll x,ll y){
return y?gcd(y,x%y):x;
} inline void dvd(ll x){
for(int i=2;i*(ll)i<=x;i++) if(!(x%i)){
d[++num]=i; ll pre=x;
while(!(x%i)) x/=i;
ans*=pre/x; if(x==1) break;
}
if(x!=1) d[++num]=x,ans*=x;
} inline void solve(){
for(int i=1;i<=num;i++){
ll pre=a;
while(!(a%d[i])) a/=d[i];
ans*=pre/a;
}
printf("%llu\n",ans);
} int main(){
scanf("%d",&T);
for(int i=1;i<=T;i++){
scanf("%llu%llu%llu",&a,&b,&L);
a=a*b/gcd(a,b);
printf("Case %d: ",i); num=0,ans=1;
if(L%a) puts("impossible");
else{
dvd(L/a);
solve();
}
} return 0;
}
LightOj 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 求最 ...
- 1215 - Finding LCM
1215 - Finding LCM LCM is an abbreviation used for Least Common Multiple in Mathematics. We say LC ...
- lightoj 1215
lightoj 1215 Finding LCM 链接:http://www.lightoj.com/volume_showproblem.php?problem=1215 题意:已知 a, b, l ...
- 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 1236 - Pairs Forming LCM (分解素因子,LCM )
题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意:给你一个数n,求有多少对(i, j)满足 LCM(i, j) = n, ...
- 专题[vjudge] - 数论0.1
专题[vjudge] - 数论0.1 web-address : https://cn.vjudge.net/contest/176171 A - Mathematically Hard 题意就是定义 ...
- LightOJ 1236 - Pairs Forming LCM(素因子分解)
B - Pairs Forming LCM Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
随机推荐
- python 面对对象基础
目录 面向对象基础 面向对象编程(抽象) 类与对象 给对象定制独有的特征 对象的属性查找顺序 类与对象的绑定方法 类与数据类型 对象的高度整合 面向对象基础 面向对象编程(抽象) 回顾一下 面向过程编 ...
- 树莓派开发板入门学习笔记1:[转]资料收集及树莓派系统在Ubuntu安装
参考教程(微雪课堂):http://www.waveshare.net/study/portal.php 树莓派实验室: http://shumeipai.nxez.com/2014/12/21/us ...
- python模块之datetime
相比于time模块,datetime模块的接口则更直观.更容易调用 datetime模块定义了下面这几个类: datetime.date:表示日期的类.常用的属性有year, month, day: ...
- 大家好,我是一个JAVA初学者,想在这里记下自己学习过程中的点点滴滴,请多多关照
大家好,我是一个JAVA初学者,想在这里记下自己学习JAVA的点点滴滴,请多多关照. 以前一直在QQ空间里记录的,但感觉有些麻烦,而且有些东西自己理解的并不完善甚至都不正确,现在开始在这里重新记录,从 ...
- Elasticsearch安装---安装jdk
1.在Linux 上检查Java版本是否满足要求: java -version 如果运行上面命令时报错:-bash: java: command not found,首先检查是否有jdk ,要是有安装 ...
- LA 5007 Detector Placement 模拟
题意: 给出一束光线(射线),和一块三角形的棱镜 以及 棱镜的折射率,问光线能否射到X轴上,射到X轴上的坐标是多少. 分析: 其实直接模拟就好了,注意到题目中说不会发生全反射,所以如果射到棱镜中的话就 ...
- python基础学习笔记——列表及元组
列表 列表的介绍 列表是python的基础数据类型之一 ,其他编程语言也有类似的数据类型. 列表的索引和切片 列表和字符串一样也拥有索引: lst = ['刘德华','周润发','周杰伦','向华强 ...
- 《小团团团队》【Alpha】Scrum Meeting 3
项目 内容 课程名 软件工程 作业链接地址 Github地址 团队名称 小团团团队 具体目标 1.掌握软件测试基础技术:2.学习迭代式增量软件开发过程(Scrum) 1.1前言 第三天 时间: 201 ...
- 【ajax】全面总结
Ajax 的全面总结 2017-11-03 山外de楼 JavaScript Ajax在前端开发中有着举足轻重的地位,关于Ajax的使用和注意事项一直是一个重要的话题,借此机会,本文希望对Ajax做一 ...
- Selenium 报错:Element is not clickable at point
WebDriverException: unknown error: Element <td class="grid - select - input " stype=&qu ...