强迫症

All Kill

Description

人行道铺着两行地砖,第一行每块的长度是 \(A/B\) ,第二行每块的长度是 \(X/Y\) 。两行砖块第一块的一边是对齐的。
作为一个强迫症患者,看到这样的地砖你很不爽,于是就想知道,最少隔多少距离后两行地砖的缝隙又会对齐。

Solution

整数的话都会吧,分数手算也会吧。
那合起来就好了。注意用long long

#include<bits/stdc++.h>
using namespace std;

#define ll long long

inline int read() {
    int x = 0, flag = 1; char ch = getchar(); while (!isdigit(ch)) { if (!(ch ^ '-')) flag = -1; ch = getchar(); }
    while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar(); return x * flag;
}

inline void write(ll x) {
    if (!x) { putchar('0'); return; } if (x < 0) putchar('-'), x = -x;
    char buf[20] = ""; int top = 0; while (x) buf[++top] = x % 10 + '0', x /= 10; while (top) putchar(buf[top--]);
}

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int main() {
    int T = read(); while(T--) {
        ll a = read(), b = read(), x = read(), y = read();
    	ll c = lcm(b, y), d = a * c / b, e = x * c / y, f = lcm(d, e), t = gcd(f, c);
    	if (!(c ^ t)) write(f / t), puts("");
    	else write(f / t), putchar('/'), write(c / t), puts("");
    }
    return 0;
}
posted @ 2018-02-05 10:05  aziint  阅读(142)  评论(0编辑  收藏  举报
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.