http://acm.hdu.edu.cn/showproblem.php?pid=1713
输入 a/b c/d
转换后变成:
(a*d)/(b*d) 和 (c*b)/(b*d)
按照题意,就是在转相同的圈子(b*d圈)时,各自需要时间a*d和c*b.
所以,这里把a*b与c*b的最小公倍数求出来就可以了。
这样。求出的最小公倍数lcm再除以(b*d)就是所求的周期。
(http://www.wutianqi.com/)
但是,这里要求若无法整出,则写出分数形式,这时,
就可以求lcm与(b*d)的最大公约数gcd,
求出gcd后与(b*d)比较,若相等,则证明可以整除~~~~
1
#include
<
cstdio
>
2 #include < iostream >
3 #include < algorithm >
4 using namespace std;
5
6 __int64 gcd(__int64 a, __int64 b)
7 {
8 if (a < b)
9 {
10 a ^= b;
11 b ^= a;
12 a ^= b;
13 }
14 if (b == 0 )
15 return a;
16 return gcd(b, a % b);
17 }
18
19 __int64 lcm(__int64 a, __int64 b)
20 {
21 return a / gcd(a, b) * b;
22 }
23
24 int main()
25 {
26 // freopen("input.txt", "r", stdin);
27 int nCases;
28 scanf( " %d " , & nCases);
29 for ( int i = 0 ; i < nCases; ++ i)
30 {
31 char tmp;
32 __int64 a, b, c, d;
33 scanf( " %I64d/%I64d %I64d/%I64d " , & a, & b, & c, & d);
34 __int64 m = a * d, n = b * c, p = b * d;
35 __int64 k = lcm(m, n);
36 int h = gcd(k, p);
37 if (p == h)
38 printf( " %I64d\n " , k / h);
39 else
40 printf( " %I64d/%I64d\n " , k / h, p / h);
41 }
42 return 0 ;
43 }
2 #include < iostream >
3 #include < algorithm >
4 using namespace std;
5
6 __int64 gcd(__int64 a, __int64 b)
7 {
8 if (a < b)
9 {
10 a ^= b;
11 b ^= a;
12 a ^= b;
13 }
14 if (b == 0 )
15 return a;
16 return gcd(b, a % b);
17 }
18
19 __int64 lcm(__int64 a, __int64 b)
20 {
21 return a / gcd(a, b) * b;
22 }
23
24 int main()
25 {
26 // freopen("input.txt", "r", stdin);
27 int nCases;
28 scanf( " %d " , & nCases);
29 for ( int i = 0 ; i < nCases; ++ i)
30 {
31 char tmp;
32 __int64 a, b, c, d;
33 scanf( " %I64d/%I64d %I64d/%I64d " , & a, & b, & c, & d);
34 __int64 m = a * d, n = b * c, p = b * d;
35 __int64 k = lcm(m, n);
36 int h = gcd(k, p);
37 if (p == h)
38 printf( " %I64d\n " , k / h);
39 else
40 printf( " %I64d/%I64d\n " , k / h, p / h);
41 }
42 return 0 ;
43 }