結果

問題 No.2066 Simple Math !
ユーザー koba-e964koba-e964
提出日時 2022-09-03 01:25:05
言語 Rust
(1.77.0)
結果
RE  
実行時間 -
コード長 3,347 bytes
コンパイル時間 13,299 ms
コンパイル使用メモリ 377,272 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 01:13:40
合計ジャッジ時間 16,487 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 7 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 209 ms
5,376 KB
testcase_05 AC 207 ms
5,376 KB
testcase_06 AC 209 ms
5,376 KB
testcase_07 AC 206 ms
5,376 KB
testcase_08 AC 206 ms
5,376 KB
testcase_09 AC 243 ms
5,376 KB
testcase_10 AC 226 ms
5,376 KB
testcase_11 AC 218 ms
5,376 KB
testcase_12 AC 226 ms
5,376 KB
testcase_13 AC 222 ms
5,376 KB
testcase_14 AC 200 ms
5,376 KB
testcase_15 AC 199 ms
5,376 KB
testcase_16 AC 197 ms
5,376 KB
testcase_17 AC 203 ms
5,376 KB
testcase_18 AC 206 ms
5,376 KB
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 AC 83 ms
5,376 KB
testcase_25 AC 79 ms
5,376 KB
testcase_26 AC 80 ms
5,376 KB
testcase_27 AC 79 ms
5,376 KB
testcase_28 AC 78 ms
5,376 KB
testcase_29 AC 23 ms
5,376 KB
testcase_30 AC 4 ms
5,376 KB
testcase_31 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
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, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); }
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) { if *self < x { *self = x; } }
    fn chmin(&mut self, x: T) { if *self > x { *self = x; } }
}

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 {
    let lim = a * b - a - b;
    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