algo — C++23 日本語リファレンス GitHub

test/aoj/challenge/1208.test.cpp

AC · 最終更新 2026-09-06 · GitHub

問題: https://onlinejudge.u-aizu.ac.jp/problems/1208

依存するヘッダ(1)
実装
// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/1208
#include <cstdint>
#include <iostream>
#include "number_theory/stern_brocot_tree.hpp"

int main(void) {
    while (true) {
        int n, p;
        std::cin >> p >> n;
        if (n == 0 && p == 0) break;
        int u, v, x, y;
        auto f = [&](std::int64_t a, std::int64_t b) -> bool {
            bool f = a * a < b * b * p;
            if (f) u = a, v = b;
            else x = a, y = b;
            return f;
        };
        stern_brocot_tree sbt;
        while (true) {
            auto [s, t] = sbt.value();
            if (s > n || t > n) break;
            if (f(s, t)) sbt = sbt.right();
            else sbt = sbt.left();
        }

        std::cout << x << "/" << y << " " << u << "/" << v << '\n';
    }

    return 0;
}