結果

問題 No.1140 EXPotentiaLLL!
ユーザー ikdikd
提出日時 2020-07-31 22:00:48
言語 Rust
(1.77.0)
結果
RE  
実行時間 -
コード長 1,137 bytes
コンパイル時間 1,088 ms
コンパイル使用メモリ 150,012 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-20 23:17:39
合計ジャッジ時間 2,662 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

use std::io::Read;

fn read<T: std::str::FromStr>() -> T {
    let token: String = std::io::stdin()
        .bytes()
        .map(|c| c.ok().unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}

fn mpow(a: i64, x: i64, mo: i64) -> i64 {
    if x == 0 {
        1
    } else if x == 1 {
        a
    } else if (x & 1) == 1 {
        a * mpow(a, x - 1, mo)
    } else {
        mpow(a * a, x / 2, mo)
    }
}

fn main() {
    let n = 5 * 100000 + 1;
    let mut is_prime = vec![true; n];
    is_prime[1] = false;
    for i in 2..n {
        if i * i >= n {
            break;
        }
        if is_prime[i] {
            let mut j = i * 2;
            while j < n {
                is_prime[j] = false;
                j += i;
            }
        }
    }

    let t: usize = read();
    for _ in 0..t {
        let a: i64 = read();
        let p: i64 = read();
        let ans = if is_prime[p as usize] {
            mpow(a, p * p / 2 % (p - 1), p)
        } else {
            -1
        };
        println!("{}", ans);
    }
}
0