Finding LCM (最小公倍数)
| Time Limit: 2000MS | Memory Limit: 32768KB | 64bit IO Format: %lld & %llu |
Description
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 bya, 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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
long long gcd(long long x,long long y)
{
if(y==)return x;
return gcd(y,x%y);
}
int main()
{
long long a,b,l;
int n,i,j;
cin>>n;
for(i=; i<=n; i++)
{
cin>>a>>b>>l;
a=a/gcd(a,b)*b;
if(l%a)
{
printf("Case %d: ",i);
printf("impossible\n");
}
else
{
b=l/a;
while(gcd(a,b)!=)
{
long long t=gcd(a,b);
b*=t;
a/=t;
}
printf("Case %d: ",i);
printf("%lld\n",b);
}
}
}
Finding LCM (最小公倍数)的更多相关文章
- LOJ Finding LCM(math)
1215 - Finding LCM Time Limit: 2 second(s) Memory Limit: 32 MB LCM is an abbreviation used for Least ...
- 1215 - Finding LCM
1215 - Finding LCM LCM is an abbreviation used for Least Common Multiple in Mathematics. We say LC ...
- Summary: gcd最大公约数、lcm最小公倍数算法
欧几里德算法 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数.其计算原理依赖于下面的定理: 定理:gcd(a,b) = gcd(b,a mod b) 证明:a可以表示成a = kb + ...
- Finding LCM LightOJ - 1215 (水题)
这题和这题一样......只不过多了个数... Finding LCM LightOJ - 1215 https://www.cnblogs.com/WTSRUVF/p/9316412.html #i ...
- LightOj 1215 - Finding LCM(求LCM(x, y)=L中的 y )
题目链接:http://lightoj.com/volume_showproblem.php?problem=1215 题意:已知三个数a b c 的最小公倍数是 L ,现在告诉你 a b L 求最 ...
- gcd,最大公约数,lcm,最小公倍数
int gcd(int a,int b){ ?a:gcd(b,a%b); } 关于lcm,若写成a*b/gcd(a,b) ,a*b可能会溢出! int lcm(int a,int b){ return ...
- LightOj 1215 Finding LCM
Discription LCM is an abbreviation used for Least Common Multiple in Mathematics. We say LCM (a, b, ...
- GCD(最大公约数)和LCM(最小公倍数)的求法
GCD(最大公约数) (1)辗转相除法(欧几里得算法)(常用) 将两个数a, b相除,如果余数c不等于0,就把b的值给a,c的值给b,直到c等于0,此时最大公约数就是b (2)更相减损术 将两个书中较 ...
- gcd和exgcd和lcm
Gcd▪ 欧几里得算法又称辗转相除法,用于计算两个正整数 a, b 的最大公约数.▪ 计算公式为 gcd(a,b) = gcd(b,a mod b).▪ 公式无需证明,记忆即可.▪ 如果要求多个数的最 ...
随机推荐
- linux(十四)之linux NFS服务管理
学到这里差不多就结束了linux的基础学习了,其实linux的内容并不难,我们要经常的反复的去操作它,多多和它去联络感情才能很好的掌握这个linux. 加油!今天是星期二.没有什么比努力让人更加热血沸 ...
- python常用标准库
-------------------系统内建函数------------------- 1.字符串 str='这是一个字符串数据测试数据'对应 str[0]:获取str字符串中下标为 ...
- postgresql如何维护WAL日志/归档日志
WAL日志介绍 wal全称是write ahead log,是postgresql中的online redo log,是为了保证数据库中数据的一致性和事务的完整性.而在PostgreSQL 7中引入的 ...
- javascript学习笔记-2:jQuery中$("xx")返回值探究
最近在写一个jQuery插件的时候,需要用到一个条件: 一组img标签,每一个元素都需要被它前面的元素值src替换,如果是第一个(序列为0)则其值为最后一个元素值,如果是最后一个,那么其值为第一个元素 ...
- html5 响应式布局(媒体查询)
响应式布局 响应式布局,简而言之,就是一个网站能够兼容多个终端--而不是为每个终端做一个特定的版本.这个概念是为解决移动互联网浏览而诞生的. 响应式布局可以为不同终端的用户 ...
- hdu 2066 最短路水题
题意:给出多个可选择的起始点和终点,求最短路 思路:执行起始点次的spfa即可 代码: #include<iostream> #include<cstdio> #include ...
- jsp---jstl配置
关于eclipse中jstl标准标签库的配置问题 我的eclipse的版本是:Version: Neon.3 Release (4.6.3) 用的1.8.0_121的jre,Tomcat用的9.0, ...
- 201521123082 《Java程序设计》第14周学习总结
201521123082 <Java程序设计>第14周学习总结 标签(空格分隔):java 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. Answ ...
- 4th-结对编程2
0x00 Coding Coding地址/小伙伴的博客地址 合作伙伴:庞伊凡(201421123011).赵娅汀(201421123012) 0x01 题目描述 上一周大家为四则运算程序设计了2-3个 ...
- 201521123020 《Java程序设计》第4周学习总结
本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. (1)类注释的使用方法是: /** *(要注释的内容) */ (2)学习了Object类,学会了覆 ...