結果
| 問題 | No.1252 数字根D | 
| コンテスト | |
| ユーザー |  Strorkis | 
| 提出日時 | 2020-10-09 22:29:50 | 
| 言語 | Rust (1.83.0 + proconio) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 2,000 ms | 
| コード長 | 1,136 bytes | 
| コンパイル時間 | 15,482 ms | 
| コンパイル使用メモリ | 378,068 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-07-20 12:47:02 | 
| 合計ジャッジ時間 | 14,679 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 1 | 
| other | AC * 14 | 
ソースコード
#[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"));
}
            
            
            
        