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 ...
随机推荐
- matlab画图颜色设置
各种颜色属性选项选项意义选项意义'r' 红色 'm' 粉红'g' 绿色 'c' 青色'b' 兰色 'w' 白色'y' 黄色 'k' 黑色各种线型属性选项选项意义选项意义'-' 实线 '--' 虚线': ...
- <原创> 通过PEB获得进程路径 (附完整工程)
完整工程:http://files.cnblogs.com/files/Gotogoo/%E8%BF%9B%E7%A8%8B%E7%AE%A1%E7%90%86%E5%99%A8%28x86%26%2 ...
- python学习-- 在for循环中还有很多有用的东西,如下:
变量 描述 forloop.counter 索引从 1 开始算 forloop.counter0 索引从 0 开始算 forloop.revcounter 索引从最大长度到 1 forloop.rev ...
- dubbo控制台在tomcat上的部署
1.下载dubbo-admin的war包,比如dubbo-admin-2.5.4.war 2.因为在tomcat上部署,所以务必确认安装了JDK和tomcat,以及配置好了环境变量. 3.将dubbo ...
- Python 开启线程的2中方式,线程VS进程(守护线程、互斥锁)
知识点一: 进程:资源单位 线程:才是CPU的执行单位 进程的运行: 开一个进程就意味着开一个内存空间,存数据用,产生的数据往里面丢 线程的运行: 代码的运行过程就相当于运行了一个线程 辅助理解:一座 ...
- localStorage的用法
1.在HTML5中,本地存储是一个window的属性,包括localStorage和sessionStorage,前者是一直存在本地的,后者是伴随着session,窗口一旦关闭就消失了.二者用法完全相 ...
- 刷题总结——run(ssoj)
题目: 题目描述 企鹅国正在举办全面运动会,第一项比赛就是跑步.N 个人在圆形跑道上跑步,他们都有各自的速度和起点.但这个跑步规则很奇怪,当两个人相遇的时候编号较小的就会出局,当场上剩下最后一个人的时 ...
- P2420 让我们异或吧 (树链剖分,异或前缀和)
题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中-xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor( B是否是男生)=A和B是否能 ...
- java面试题之为什么hashmap的数组初始化大小都是2的N次方?
当数组长度为2的N次方时,不同的key算出的index相同的几率小,数据在数组上分配均匀,hash碰撞的几率小,提升查询效率,从大O(N)提升至O(1):
- Java面试题之Java中==和equals()和hashCode()的区别
“==”: ==是运算符,用来比较两个值.两个对象的内存地址是否相等: “equals()”: equals是Object类的方法,默认情况下比较两个对象是否是同一个对象,内部实现是通过“==”来实现 ...