相遇周期
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1943 Accepted Submission(s): 1000
Problem Description
2007年3月26日,在中俄两国元首的见证下,中国国家航天局局长孙来燕与俄罗斯联邦航天局局长别尔米诺夫共同签署了《中国国家航天局和俄罗斯联邦航天局关于联合探测火星-火卫一合作的协议》,确定中俄双方将于2009年联合对火星及其卫星“火卫一”进行探测。
而卫星是进行这些探测的重要工具,我们的问题是已知两颗卫星的运行周期,求它们的相遇周期。
而卫星是进行这些探测的重要工具,我们的问题是已知两颗卫星的运行周期,求它们的相遇周期。
Input
输入数据的第一行为一个正整数T, 表示测试数据的组数. 然后是T组测试数据. 每组测试数据包含两组正整数,用空格隔开。每组包含两个正整数,表示转n圈需要的天数(26501/6335,表示转26501圈要6335天),用'/'隔开。
Output
对于每组测试数据, 输出它们的相遇周期,如果相遇周期是整数则用整数表示,否则用最简分数表示。
Sample Input
2 26501/6335 18468/42 29359/11479 15725/19170
Sample Output
81570078/7 5431415
解析:两周期分别为a/b、c/d,既然是相遇周期,就是最早什么时间相遇,也就是两者的最小公倍数,转化一下,也就是求LCM(a, c)/ gcd(b, d)。直接求即可,不一定要用long long 或者 __int64,int也能过。注意:先把a/b和c/d尽可能的化简再求。
AC代码:
#include <cstdio>
#include <iostream>
using namespace std;
long long gcd(long long a, long long b){
return b ? gcd(b, a % b) : a;
}
int main(){
// freopen("in.txt", "r", stdin);
int t;
long long a, b, c, d, x, y;
scanf("%d", &t);
while(t--){
scanf("%lld/%lld", &a, &b);
scanf("%lld/%lld", &c, &d);
long long fo = gcd(a, b);
a /= fo; //化简
b /= fo;
fo = gcd(c, d);
c /= fo;
d /= fo;
x = (a * c) / gcd(a, c); //LCM(a, c)
y = gcd(b, d);
if(x % y) printf("%lld/%lld\n", x, y);
else printf("%lld\n", x / y);
}
return 0;
}