結果

問題 No.1092 modular arithmetic
ユーザー phsplsphspls
提出日時 2020-06-28 17:48:24
言語 Rust
(1.77.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 1,111 bytes
コンパイル時間 997 ms
コンパイル使用メモリ 142,896 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 05:51:16
合計ジャッジ時間 5,614 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 7 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 32 ms
4,376 KB
testcase_04 AC 28 ms
4,380 KB
testcase_05 AC 28 ms
4,376 KB
testcase_06 AC 21 ms
4,376 KB
testcase_07 AC 27 ms
4,380 KB
testcase_08 AC 30 ms
4,376 KB
testcase_09 AC 26 ms
4,380 KB
testcase_10 AC 23 ms
4,376 KB
testcase_11 AC 25 ms
4,376 KB
testcase_12 AC 21 ms
4,380 KB
testcase_13 AC 4 ms
4,376 KB
testcase_14 AC 4 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 4 ms
4,376 KB
testcase_18 AC 33 ms
4,380 KB
testcase_19 AC 20 ms
4,380 KB
testcase_20 AC 34 ms
4,376 KB
testcase_21 AC 26 ms
4,380 KB
testcase_22 AC 33 ms
4,376 KB
testcase_23 AC 84 ms
4,380 KB
testcase_24 AC 27 ms
4,376 KB
testcase_25 AC 65 ms
4,380 KB
testcase_26 AC 82 ms
4,376 KB
testcase_27 AC 13 ms
4,376 KB
testcase_28 AC 66 ms
4,380 KB
testcase_29 AC 100 ms
4,376 KB
testcase_30 AC 66 ms
4,380 KB
testcase_31 AC 9 ms
4,376 KB
testcase_32 AC 58 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn get_mod_pow(a: usize, n: usize, p: usize) -> usize {
    if n == 0 { return 1; }
    if n == 1 { return a % p; }
    let val = get_mod_pow(a, n / 2, p) % p;
    return val * val % p * get_mod_pow(a, n % 2, p) % p;
}

//TODO
fn main() {
    let mut pn = String::new();
    std::io::stdin().read_line(&mut pn).ok();
    let pn: Vec<usize> = pn.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let p = pn[0];
    let mut a = String::new();
    std::io::stdin().read_line(&mut a).ok();
    let mut a = a.trim().split_whitespace().map(|s| s.parse::<usize>().unwrap());
    let mut result: usize = a.next().unwrap();
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().chars().zip(a).for_each(|pair| {
        match pair.0 {
            '+' => { result += pair.1; result %= p; },
            '-' => { result += pair.1 * p; result -= pair.1; result %= p; },
            '*' => { result *= pair.1; result %= p; },
            '/' => { result *= get_mod_pow(pair.1, p-2, p); result %= p; },
            _ => {}
        }
    });

    println!("{}", result);
}
0