結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー iwkjoseciwkjosec
提出日時 2019-06-02 16:50:53
言語 Rust
(1.77.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,385 bytes
コンパイル時間 16,324 ms
コンパイル使用メモリ 379,272 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 13:13:55
合計ジャッジ時間 14,564 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 159 ms
5,376 KB
testcase_05 AC 154 ms
5,376 KB
testcase_06 AC 71 ms
5,376 KB
testcase_07 AC 70 ms
5,376 KB
testcase_08 AC 69 ms
5,376 KB
testcase_09 AC 277 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io;

fn main() {
    let mut buf = String::new();
    io::stdin().read_line(&mut buf).unwrap();
    let n: i32 = buf.trim().parse().unwrap();

    for _ in 0..n {
        let mut buf = String::new();
        io::stdin().read_line(&mut buf).unwrap();
        let i: i64 = buf.trim().parse().unwrap();

        println!("{} {}", i, if miller_rabin(i) { 1 } else { 0 });
    }
}
const BASES64: [i128; 7] = [2, 325, 9375, 28178, 450775, 9780504, 1795265022];

#[no_mangle]
pub extern "C" fn miller_rabin(n: i64) -> bool {
    if n < 2 {
        return false;
    }
    if (n & 1) == 0 {
        return n == 2;
    }
    let mut d = n - 1;
    let n = n as i128;
    let mut s = 0;
    while (d & 1) == 0 {
        s += 1;
        d >>= 1;
    }
    for a in BASES64.iter() {
        if *a >= n {
            break;
        }
        let mut c = true;
        let mut x = mod_pow(*a, d, n);
        if x == 1 {
            c = false;
        }
        for _ in 0..s {
            if x == n - 1 {
                c = false;
            }
            x = x * x % n;
        }
        if c {
            return false;
        }
    }
    return true;
}

fn mod_pow(mut x: i128, mut n: i64, m: i128) -> i128 {
    let mut res = 1i128;
    while n != 0 {
        if (n & 1) == 1 {
            res = res * x % m;
        }
        x = x * x % m;
        n >>= 1;
    }
    return res;
}
0