結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー 👑 MizarMizar
提出日時 2022-09-06 18:31:15
言語 Rust
(1.77.0)
結果
AC  
実行時間 205 ms / 9,973 ms
コード長 2,451 bytes
コンパイル時間 13,202 ms
コンパイル使用メモリ 398,560 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-01 14:23:44
合計ジャッジ時間 14,842 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 118 ms
6,944 KB
testcase_05 AC 111 ms
6,944 KB
testcase_06 AC 44 ms
6,940 KB
testcase_07 AC 41 ms
6,944 KB
testcase_08 AC 43 ms
6,944 KB
testcase_09 AC 205 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// -*- coding:utf-8-unix -*-
pub fn modmul(a: u64, b: u64, n: u64) -> u64 {
    debug_assert!(a < n);
    debug_assert!(b < n);
    // inline assembly: Rust 1.59 or later (stable)
    if cfg!(target_arch = "x86_64") {
        use std::arch::asm;
        let mut x: u64;
        unsafe{asm!(
            "mul {0}",
            "div {1}",
            in(reg) b,
            in(reg) n,
            inout("rax") a => _,
            out("rdx") x,
            options(pure, nomem, nostack),
        )}
        debug_assert_eq!(((a as u128) * (b as u128) % (n as u128)) as u64, x);
        x
    } else {
        ((a as u128) * (b as u128) % (n as u128)) as u64
    }
}
pub fn modpow(mut b: u64, mut p: u64, n: u64) -> u64 {
    let mut r = if (p & 1) == 0 { 1 } else { b };
    loop {
        p >>= 1; if p == 0 { return r; }
        b = modmul(b, b, n); if p & 1 != 0 { r = modmul(r, b, n); }
    }
}
pub fn miller_rabin(n: u64) -> bool {
    if n == 2 { return true; }
    if n < 2 || n & 1 == 0 { return false; }
    let n1 = n - 1;
    let s = n1.trailing_zeros();
    let d = n1 >> s;
    [2,325,9375,28178,450775,9780504,1795265022].iter().all(|&base| {
        let a = if base < n { base } else { base % n };
        if a == 0 { return true; }
        let mut t = modpow(a, d, n);
        if t == 1 || t == n1 { return true; }
        for _ in 1..s { t = modmul(t, t, n); if t == n1 { return true; } }
        false
    })
}
pub fn main() {
    use std::io::{stdin, stdout, BufReader, BufRead, BufWriter, Write};
    let start_time = std::time::Instant::now();
    let out = stdout();
    let mut writer = BufWriter::new(out.lock());
    let inp = stdin();
    let mut reader = BufReader::new(inp.lock());
    let mut ibuf = Vec::<u8>::new();
    let len = reader.read_until(b'\n', &mut ibuf).unwrap();
    let tr = &ibuf[0..=((0..len).rfind(|&i| ibuf[i] > 0x20).unwrap())];
    let n = tr.iter().fold(0, |a, &c| a * 10 + (c & 0x0f) as usize);
    for _ in 0..n {
        ibuf.clear();
        let len = reader.read_until(b'\n', &mut ibuf).unwrap();
        let len = (0..len).rfind(|&i| ibuf[i] > 0x20).unwrap();
        let tr = &ibuf[0..=len];
        let x = tr.iter().fold(0, |a, &c| a * 10 + (c & 0x0f) as u64);
        let res = miller_rabin(x);
        writer.write(tr).unwrap();
        writer.write(if res { b" 1\n" } else { b" 0\n" }).unwrap();
    }
    writer.flush().unwrap();
    eprint!("{}us\n", start_time.elapsed().as_micros());
}
0