LOJ Finding LCM(math)
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)的更多相关文章
- Finding LCM (最小公倍数)
Finding LCM Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu [Submit] ...
- Finding LCM LightOJ - 1215 (水题)
这题和这题一样......只不过多了个数... Finding LCM LightOJ - 1215 https://www.cnblogs.com/WTSRUVF/p/9316412.html #i ...
- 1215 - Finding LCM
1215 - Finding LCM LCM is an abbreviation used for Least Common Multiple in Mathematics. We say LC ...
- 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{ ...
- 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, ...
- 【原创】开源Math.NET基础数学类库使用(09)相关数论函数使用
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...
- 开源Math.NET基础数学类库使用(09)相关数论函数使用
原文:[原创]开源Math.NET基础数学类库使用(09)相关数论函数使用 本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4 ...
- 专题[vjudge] - 数论0.1
专题[vjudge] - 数论0.1 web-address : https://cn.vjudge.net/contest/176171 A - Mathematically Hard 题意就是定义 ...
随机推荐
- 重构第25天 引入契约设计(Introduce Design By Contract checks)
理解:本文中的”引入契约式设计”是指我们应该对应该对输入和输出进行验证,以确保系统不会出现我们所想象不到的异常和得不到我们想要的结果. 详解:契约式设计规定方法应该对输入和输出进行验证,这样你便可以保 ...
- {"集合已修改;可能无法执行枚举操作。"}
无论是向集合中添加元素还是从集合中删除元素,都会导致集合内部的变化,特别是集合遍历器的变化.例如 List<,,,,}; foreach(int x in list) { list.Remove ...
- (转) 关于在IE6下 无法跳转问题
之前在项目,用到超链接,在ie下没有问题,但是到了ie6,居然发现点击事件不起作用, 真不可思议,以前都没注意到,后来网上搜了下,问题就出在这个void(0)上!现把网上的资料整理了下. <a ...
- WPF 程序自删除(自毁)|卸载程序删除
一般是在MainWindow_Closed 事件中调用批处理命令删除. 在借鉴别人的想法的基础上的算是改进. 自删除步骤: 1.删除文件 2.删除存放文件夹. 实现代码: private static ...
- php中的字符串常用函数(一) strpos() 子字符首次出现的位置
strpos($str, $needle); 1.返回$needle在$str首次出现的位置.(大小写敏感). 2.从php5开始$needle支持多字符.php4只能用单个字符. 3.能找到$nee ...
- PHP学习笔记:用php读取xml文件
xml已经被json逐渐替代,现在用的api都是用貌似用的json,但是有些老的网站还是在用xml. 这里默认xml文件为:address.xml,存放在和读取的php文件相同级别目录,xml内容如下 ...
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
最近angularjs post到后台 400一头雾水 没有任何错误. 最后发现好文,感谢作者 SpringMVC中出现" 400 Bad Request "错误(用@Respon ...
- 常用Keytool 命令
常用Keytool 命令Keytool 是一个JAVA环境下的安全钥匙与证书的管理工具.它管理一个存储了私有钥匙和验证相应公共钥匙的与它们相关联的X.509 证书链的keystore(相当一个数据库, ...
- CentOS6.5 安装Zookeeper集群
1.下载解压 2.配置环境变量:vi ~/.bashrc 或者 vi /etc/profile [hadoopuser@Linux01 ~]$ vi ~/.bashrc # zookeeper ...
- 快速理解JS的闭包
/**闭包:1.在函数内部改变变量值,不影响函数外全局变量(相当于JAVA中私有变量)* 2.调用闭包后,最后产生的变量值并不释放.* 3.任何人调用闭包,闭包里面的值并不 ...