結果

問題 No.2066 Simple Math !
ユーザー koba-e964koba-e964
提出日時 2022-09-03 01:28:40
言語 Rust
(1.77.0)
結果
AC  
実行時間 244 ms / 2,000 ms
コード長 2,976 bytes
コンパイル時間 13,907 ms
コンパイル使用メモリ 378,232 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 01:15:40
合計ジャッジ時間 18,583 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 4 ms
5,376 KB
testcase_02 AC 8 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 232 ms
5,376 KB
testcase_05 AC 231 ms
5,376 KB
testcase_06 AC 232 ms
5,376 KB
testcase_07 AC 231 ms
5,376 KB
testcase_08 AC 231 ms
5,376 KB
testcase_09 AC 243 ms
5,376 KB
testcase_10 AC 244 ms
5,376 KB
testcase_11 AC 241 ms
5,376 KB
testcase_12 AC 242 ms
5,376 KB
testcase_13 AC 243 ms
5,376 KB
testcase_14 AC 224 ms
5,376 KB
testcase_15 AC 223 ms
5,376 KB
testcase_16 AC 224 ms
5,376 KB
testcase_17 AC 223 ms
5,376 KB
testcase_18 AC 224 ms
5,376 KB
testcase_19 AC 58 ms
5,376 KB
testcase_20 AC 58 ms
5,376 KB
testcase_21 AC 58 ms
5,376 KB
testcase_22 AC 59 ms
5,376 KB
testcase_23 AC 59 ms
5,376 KB
testcase_24 AC 89 ms
5,376 KB
testcase_25 AC 89 ms
5,376 KB
testcase_26 AC 89 ms
5,376 KB
testcase_27 AC 89 ms
5,376 KB
testcase_28 AC 89 ms
5,376 KB
testcase_29 AC 25 ms
5,376 KB
testcase_30 AC 5 ms
5,376 KB
testcase_31 AC 4 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

fn gcd(mut x: i64, mut y: i64) -> i64 {
    while y != 0 {
        let r = x % y;
        x = y;
        y = r;
    }
    x
}

// Ported from ACL.
// Computes \sum_{i = 0}^{n - 1} floor((a * i + b) / m).
// Verified by: https://atcoder.jp/contests/arc111/submissions/23877969
fn floor_sum(n: i64, m: i64, a: i64, b: i64) -> i64 {
    fn internal(n: i64, m: i64, mut a: i64, mut b: i64, mut acc: i64) -> i64 {
        if a >= m {
            let q = a / m;
            acc += (n - 1) * n / 2 * q;
            a -= q * m;
        }
        if b >= m {
            let q = b / m;
            acc += n * q;
            b -= q * m;
        }
        let y_max = (a * n + b) / m;
        let x_max = y_max * m - b;
        if y_max == 0 {
            return acc;
        }
        acc += (n - (x_max + a - 1) / a) * y_max;
        let mut sub_b = a - x_max % a;
        if sub_b >= a {
            sub_b -= a;
        }
        internal(y_max, a, m, sub_b, acc)
    }
    internal(n, m, a, b, 0)
}

// gcd(a, b) = 1
fn calc(a: i64, b: i64, k: i64) -> i64 {
    if a == 1 || b == 1 {
        return k;
    }
    let lim = a * b - a - b; // lim >= 0
    let cnt = floor_sum(lim / b + 1, a, b, lim - (lim / b) * b) + lim / b;
    if cnt < k {
        return lim + k - cnt;
    }
    let mut fail = -1;
    let mut pass = lim;
    while pass - fail > 1 {
        let mid = (pass + fail) / 2;
        let tmp = floor_sum(mid / b + 1, a, b, mid - (mid / b) * b) + mid / b;
        if tmp >= k {
            pass = mid;
        } else {
            fail = mid;
        }
    }
    pass
}

fn main() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
    input! {
        t: usize,
        pqk: [(i64, i64, i64); t],
    }
    for (p, q, k) in pqk {
        let g = gcd(p, q);
        puts!("{}\n", calc(p / g, q / g, k) * g);
    }
}
0