結果

問題 No.2183 LCA on Rational Tree
ユーザー t98slidert98slider
提出日時 2023-01-08 18:18:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 583 ms / 2,000 ms
コード長 1,578 bytes
コンパイル時間 1,644 ms
コンパイル使用メモリ 166,896 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-22 03:59:25
合計ジャッジ時間 4,289 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 509 ms
4,380 KB
testcase_02 AC 575 ms
4,380 KB
testcase_03 AC 319 ms
4,380 KB
testcase_04 AC 85 ms
4,384 KB
testcase_05 AC 583 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int Q;
    cin >> Q;
    /*vector<int> prime;
    vector<bool> tb(32000);
    for(int i = 2; i < 32000; i++){
        if(tb[i])continue;
        prime.push_back(i);
        for(int j = 2 * i; j < 32000; j += i){
            tb[j] = true;
        }
    }*/
    while(Q--){
        int p0, q0, p1, q1;
        cin >> p0 >> q0 >> p1 >> q1;
        auto ope = [&](int &p, int &q, int d){
            int p2 = p0 ^ p1 ^ p, q2 = q0 ^ q1 ^ q, d2 = q2 - p2;
            int di = 1 << 30;
            for(int i = 1; i * i <= d; i++){
                if(d % i == 0){
                    if(i != 1)di = min(di, i - p % i);
                    int v = d / i;
                    di = min(di, v - p % v);
                }
            }
            if(d == d2 && p2 <= p + di){
                p = p2, q = q2;
                return;
            }
            p += di, q += di;
            int g = __gcd(p, q);
            p /= g, q /= g;
        };
        while(p0 != p1 || q0 != q1){
            int d0 = q0 - p0;
            int d1 = q1 - p1;
            if(d0 == 1 && d1 == 1){
                p0 = max(p0, p1);
                q0 = p0 + 1;
                break;
            }
            if(d0 > d1){
                ope(p0, q0, d0);
            }else if(d1 > d0){
                ope(p1, q1, d1);
            }else{
                if(p0 <= p1)ope(p0, q0, d0);
                else ope(p1, q1, d1);
            }
        }
        cout << p0 << " " << q0 << '\n';
    }
}
0