結果

問題 No.1252 数字根D
ユーザー StrorkisStrorkis
提出日時 2020-10-09 22:29:50
言語 Rust
(1.77.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,136 bytes
コンパイル時間 5,130 ms
コンパイル使用メモリ 141,608 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 18:17:49
合計ジャッジ時間 3,118 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_macros)]
macro_rules! read_to_tuple {
    ( $( $t:ty ),* ) => {{
        let mut input = String::new();
        std::io::stdin().read_line(&mut input).unwrap();
        let mut iter = input.split_whitespace();
        ( $( iter.next().unwrap().parse::<$t>().unwrap() ),* )
    }}
}

fn f(d: i64, mut n: i64) -> i64 {
    let mut res = 0;
    while n != 0 {
        res += n % d;
        n /= d;
    }
    res
}

fn ff(d: i64, mut n: i64) -> i64 {
    let mut m = f(d, n);
    while m != n {
        n = m;
        m = f(d, n);
    }
    n
}

fn solve() -> String {
    let (d, a, b) = read_to_tuple!(i64, i64, i64);

    let mut c = b - a + 1;
    let n = ff(d, a);
    let ans = if c <= d - n {
        (n + (n + c - 1)) * c / 2
    } else {
        let mut res = (n + (d - 1)) * (d - n) / 2;
        c -= d - n;
        res += (1 + (d - 1)) * (d - 1) / 2 * (c / (d - 1));
        c %= d - 1;
        res += (1 + c) * c / 2;
        res
    };

    ans.to_string()
}

fn main() {
    let t = read_to_tuple!(usize);
    let answers = (0..t).map(|_| solve()).collect::<Vec<_>>();
    println!("{}", answers.join("\n"));
}
0