結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー 👑 MizarMizar
提出日時 2022-08-17 03:48:05
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 227 ms / 9,973 ms
コード長 5,733 bytes
コンパイル時間 13,534 ms
コンパイル使用メモリ 387,580 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-11-16 23:52:13
合計ジャッジ時間 14,776 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 1 ms
6,816 KB
testcase_04 AC 127 ms
6,816 KB
testcase_05 AC 123 ms
6,816 KB
testcase_06 AC 51 ms
6,820 KB
testcase_07 AC 50 ms
6,824 KB
testcase_08 AC 50 ms
6,816 KB
testcase_09 AC 227 ms
6,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// -*- coding:utf-8-unix -*-

#[allow(unused_imports)]
use std::io::{BufReader, BufWriter, Write, stdin, stdout, stderr};
#[allow(unused_imports)]
use std::collections::*;
#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;

// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); }
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) { if *self < x { *self = x; } }
    fn chmin(&mut self, x: T) { if *self > x { *self = x; } }
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    input! {
        n: usize,
        x: [u64; n],
    }
    macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
    for i in 0..n {
        puts!("{} {}\n", x[i], if prime_test(x[i]) { 1 } else { 0 });
    }
    out.flush().unwrap();
}

fn prime_test(n: u64) -> bool {
    match n {
        // The best known SPRP bases sets
        // http://miller-rabin.appspot.com/
        0..=341531 => {
            prime_test_intl(n, 9345883071009581737)
        },
        0..=1050535501 => {
            prime_test_intl(n, 336781006125) &&
            prime_test_intl(n, 9639812373923155)
        },
        0..=350269456337 => {
            prime_test_intl(n, 4230279247111683200) &&
            prime_test_intl(n, 14694767155120705706) &&
            prime_test_intl(n, 16641139526367750375)
        },
        0..=55245642489451 => {
            prime_test_intl(n, 2) &&
            prime_test_intl(n, 141889084524735) &&
            prime_test_intl(n, 1199124725622454117) &&
            prime_test_intl(n, 11096072698276303650)
        },
        0..=7999252175582851 => {
            prime_test_intl(n, 2) &&
            prime_test_intl(n, 4130806001517) &&
            prime_test_intl(n, 149795463772692060) &&
            prime_test_intl(n, 186635894390467037) &&
            prime_test_intl(n, 3967304179347715805)
        },
        0..=585226005592931977 => {
            prime_test_intl(n, 2) &&
            prime_test_intl(n, 123635709730000) &&
            prime_test_intl(n, 9233062284813009) &&
            prime_test_intl(n, 43835965440333360) &&
            prime_test_intl(n, 761179012939631437) &&
            prime_test_intl(n, 1263739024124850375)
        },
        _ => {
            prime_test_intl(n, 2) &&
            prime_test_intl(n, 325) &&
            prime_test_intl(n, 9375) &&
            prime_test_intl(n, 28178) &&
            prime_test_intl(n, 450775) &&
            prime_test_intl(n, 9780504) &&
            prime_test_intl(n, 1795265022)
        },
    }
}
fn prime_test_intl(n: u64, base: u64) -> bool {
    if n == 2 { return true; }
    if n == 1 || (n & 1) == 0 { return false; }
    let mut d = n - 1;
    let k = d.trailing_zeros();
    d >>= k;
    assert!((d << k) + 1 == n);
    assert!((d & 1) == 1);
    let mut b = U64Mod { n: base % n, m: n };
    if b.n == 0 { return true; }
    b = b.powi(d);
    if b.n == 1 { return true; }
    for _ in 0..k {
        if b.n == (n - 1) { return true; }
        b = b.sq();
    }
    false
}

#[derive(Clone, Copy, Debug)]
pub struct U64Mod {
    n: u64,
    m: u64,
}
impl U64Mod {
    fn sq(&self) -> Self {
        debug_assert!(self.n < self.m);
        let m = self.m;
        Self {
            n: match m {
                0..=0xffff_ffff => (self.n * self.n) % m,
                _ => (((self.n as u128) * (self.n as u128)) % (m as u128)) as u64,
            },
            m,
        }
    }
    fn powi(&self, mut b: u64) -> Self {
        debug_assert!(self.n < self.m);
        let m = self.m;
        let mut p = self.n;
        let mut n = if (b & 1) == 1 { self.n } else { 1 };
        b >>= 1;
        while b > 0 {
            p = match m { 
                0..=0xffff_ffff => (p * p) % m,
                _ => (((p as u128) * (p as u128)) % (m as u128)) as u64,
            };
            if (b & 1) == 1 {
                n = match m {
                    0..=0xffff_ffff => (n * p) % m,
                    _ => (((n as u128) * (p as u128)) % (m as u128)) as u64,
                };
            }
            b >>= 1;
        }
        Self { n, m }
    }
}
0