This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "../../../../template/template.hpp"
#define PROBLEM "https://judge.yosupo.jp/problem/stern_brocot_tree"
#include "../../../../math/number-theory/SternBrocotTree.hpp"
int main() {
cin.tie(0)->sync_with_stdio(0);
int t;
in(t);
rep(i, t) {
string q;
in(q);
if (q == "ENCODE_PATH") {
int a, b;
in(a, b);
auto res = encode_path(a, b);
if (res.empty()) {
out(0);
} else {
cout << res.size() << " ";
rep(j, res.size()) {
cout << res[j].first << " " << res[j].second;
if (j + 1 != res.size()) {
cout << " ";
} else {
cout << "\n";
}
}
}
} else if (q == "DECODE_PATH") {
int k;
in(k);
vector<pair<char, int>> path(k);
rep(j, k) {
in(path[j].first, path[j].second);
}
auto res = decode_path(path);
out(res.first, res.second);
} else if (q == "LCA") {
int a, b, c, d;
in(a, b, c, d);
auto res = sbt_lca(a, b, c, d);
out(res.first, res.second);
} else if (q == "ANCESTOR") {
int k, a, b;
in(k, a, b);
auto res = sbt_ancestor(k, a, b);
if (res.first == -1) {
out(-1);
} else {
out(res.first, res.second);
}
} else {
int a, b;
in(a, b);
auto res = sbt_range(a, b);
out(res[0].first, res[0].second, res[1].first, res[1].second);
}
}
}#line 2 "template/template.hpp"
#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using ull = unsigned long long;
using ld = long double;
using int128 = __int128_t;
#define all(x) (x).begin(), (x).end()
#define uniqv(v) v.erase(unique(all(v)), v.end())
#define OVERLOAD_REP(_1, _2, _3, name, ...) name
#define REP1(i, n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define logfixed(x) cout << fixed << setprecision(10) << x << endl;
ostream &operator<<(ostream &dest, __int128_t value) {
ostream::sentry s(dest);
if (s) {
__uint128_t tmp = value < 0 ? -value : value;
char buffer[128];
char *d = end(buffer);
do {
--d;
*d = "0123456789"[tmp % 10];
tmp /= 10;
} while (tmp != 0);
if (value < 0) {
--d;
*d = '-';
}
int len = end(buffer) - d;
if (dest.rdbuf()->sputn(d, len) != len) {
dest.setstate(ios_base::badbit);
}
}
return dest;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); i++) {
os << v[i] << (i + 1 != (int)v.size() ? " " : "");
}
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &set_var) {
for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
os << *itr;
++itr;
if (itr != set_var.end()) os << " ";
itr--;
}
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &set_var) {
for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
os << *itr;
++itr;
if (itr != set_var.end()) os << " ";
itr--;
}
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
os << itr->first << " -> " << itr->second << "\n";
}
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const unordered_map<T, U> &map_var) {
for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
os << itr->first << " -> " << itr->second << "\n";
}
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &pair_var) {
os << pair_var.first << " " << pair_var.second;
return os;
}
void out() { cout << '\n'; }
template <class T, class... Ts>
void out(const T &a, const Ts &...b) {
cout << a;
(cout << ... << (cout << ' ', b));
cout << '\n';
}
void outf() { cout << '\n'; }
template <class T, class... Ts>
void outf(const T &a, const Ts &...b) {
cout << fixed << setprecision(14) << a;
(cout << ... << (cout << ' ', b));
cout << '\n';
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v) is >> in;
return is;
}
inline void in(void) { return; }
template <typename First, typename... Rest>
void in(First &first, Rest &...rest) {
cin >> first;
in(rest...);
return;
}
template <typename T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T>
bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
vector<lint> dx8 = {1, 1, 0, -1, -1, -1, 0, 1};
vector<lint> dy8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<lint> dx4 = {1, 0, -1, 0};
vector<lint> dy4 = {0, 1, 0, -1};
#pragma endregion
#line 2 "verify/LibraryChecker/math/number-theory/SternBrocotTree.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/stern_brocot_tree"
#line 1 "math/number-theory/SternBrocotTree.hpp"
// m/n
vector<pair<char, int>> encode_path(lint m, lint n) {
vector<pair<char, int>> res;
while (m != 1 or n != 1) {
if (m < n) {
int num = (n - 1) / m;
res.emplace_back('L', num);
n -= m * num;
} else {
int num = (m - 1) / n;
res.emplace_back('R', num);
m -= n * num;
}
}
return res;
}
pair<long long, long long> decode_path(const vector<pair<char, int>>& path) {
long long n = 1;
long long m = 0;
long long n_ = 0;
long long m_ = 1;
if (path.empty()) return {1, 1};
for (const auto [c, num] : path) {
if (c == 'L') {
n_ += n * num;
m_ += m * num;
} else {
n += n_ * num;
m += m_ * num;
}
}
return {m + m_, n + n_};
}
// a/b, c/d
pair<long long, long long> sbt_lca(long long a, long long b, long long c, long long d) {
vector<pair<char, int>> p1 = encode_path(a, b);
vector<pair<char, int>> p2 = encode_path(c, d);
int siz = min(int(p1.size()), int(p2.size()));
if (siz == 0) return {1, 1};
long long n = 1;
long long m = 0;
long long n_ = 0;
long long m_ = 1;
for (int i = 0; i < siz; i++) {
if (p1[i].first != p2[i].first) {
break;
} else {
long long num = min(p1[i].second, p2[i].second);
if (p1[i].first == 'L') {
n_ += n * num;
m_ += m * num;
} else {
n += n_ * num;
m += m_ * num;
}
if (p1[i].second != p2[i].second) break;
}
}
return {m + m_, n + n_};
}
// a/b
pair<long long, long long> sbt_ancestor(int k, long long a, long long b) {
vector<pair<char, int>> path = encode_path(a, b);
long long n = 1;
long long m = 0;
long long n_ = 0;
long long m_ = 1;
for (auto [c, num] : path) {
long long len = min(k, num);
if (c == 'L') {
n_ += n * len;
m_ += m * len;
} else {
n += n_ * len;
m += m_ * len;
}
k -= len;
if (k == 0) break;
}
if (k == 0) {
return {m + m_, n + n_};
} else {
return {-1, 1};
}
}
vector<pair<long long, long long>> sbt_range(long long a, long long b) {
vector<pair<char, int>> path = encode_path(a, b);
long long n = 1;
long long m = 0;
long long n_ = 0;
long long m_ = 1;
if (path.empty()) return {{0, 1}, {1, 0}};
for (const auto [c, num] : path) {
if (c == 'L') {
n_ += n * num;
m_ += m * num;
} else {
n += n_ * num;
m += m_ * num;
}
}
return {{m, n}, {m_, n_}};
}
#line 4 "verify/LibraryChecker/math/number-theory/SternBrocotTree.test.cpp"
int main() {
cin.tie(0)->sync_with_stdio(0);
int t;
in(t);
rep(i, t) {
string q;
in(q);
if (q == "ENCODE_PATH") {
int a, b;
in(a, b);
auto res = encode_path(a, b);
if (res.empty()) {
out(0);
} else {
cout << res.size() << " ";
rep(j, res.size()) {
cout << res[j].first << " " << res[j].second;
if (j + 1 != res.size()) {
cout << " ";
} else {
cout << "\n";
}
}
}
} else if (q == "DECODE_PATH") {
int k;
in(k);
vector<pair<char, int>> path(k);
rep(j, k) {
in(path[j].first, path[j].second);
}
auto res = decode_path(path);
out(res.first, res.second);
} else if (q == "LCA") {
int a, b, c, d;
in(a, b, c, d);
auto res = sbt_lca(a, b, c, d);
out(res.first, res.second);
} else if (q == "ANCESTOR") {
int k, a, b;
in(k, a, b);
auto res = sbt_ancestor(k, a, b);
if (res.first == -1) {
out(-1);
} else {
out(res.first, res.second);
}
} else {
int a, b;
in(a, b);
auto res = sbt_range(a, b);
out(res[0].first, res[0].second, res[1].first, res[1].second);
}
}
}