結果
| 問題 | No.2183 LCA on Rational Tree |
| ユーザー |
|
| 提出日時 | 2023-01-08 18:18:15 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 584 ms / 2,000 ms |
| コード長 | 1,578 bytes |
| 記録 | |
| コンパイル時間 | 1,822 ms |
| コンパイル使用メモリ | 169,872 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-12-15 21:48:12 |
| 合計ジャッジ時間 | 4,188 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 6 |
ソースコード
#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';
}
}