結果
問題 | No.3030 ミラー・ラビン素数判定法のテスト |
ユーザー | 👑 Mizar |
提出日時 | 2022-08-17 02:51:45 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 230 ms / 9,973 ms |
コード長 | 6,409 bytes |
コンパイル時間 | 14,581 ms |
コンパイル使用メモリ | 387,584 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-11-16 23:52:43 |
合計ジャッジ時間 | 14,851 ms |
ジャッジサーバーID (参考情報) |
judge1 / 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 | 129 ms
6,820 KB |
testcase_05 | AC | 125 ms
6,816 KB |
testcase_06 | AC | 51 ms
6,820 KB |
testcase_07 | AC | 50 ms
6,816 KB |
testcase_08 | AC | 50 ms
6,820 KB |
testcase_09 | AC | 230 ms
6,816 KB |
ソースコード
// -*- 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 }); } } 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, } #[allow(unused)] impl U64Mod { fn zero(m: u64) -> Self { assert!(m > 1); Self { n: 0, m } } fn one(m: u64) -> Self { assert!(m > 1); Self { n: 1, m } } fn add(&self, b: &Self) -> Self { assert_eq!(self.m, b.m); assert!(self.n < self.m); assert!(b.n < b.m); let m = self.m; let res = self.n.overflowing_add(b.n); if res.1 || res.0 >= m { Self { n: res.0.wrapping_sub(m), m } } else { Self { n: res.0, m } } } fn sub(&self, b: &Self) -> Self { assert_eq!(self.m, b.m); assert!(self.n < self.m); assert!(b.n < b.m); let m = self.m; let res = self.n.overflowing_sub(b.n); if res.1 { Self { n: res.0.wrapping_add(m), m } } else { Self { n: res.0, m } } } fn mul(&self, b: &Self) -> Self { assert_eq!(self.m, b.m); assert!(self.n < self.m); assert!(b.n < b.m); let m = self.m; let res = (self.n as u128) * (b.n as u128) % (m as u128); Self { n: res as u64, m } } fn sq(&self) -> Self { assert!(self.n < self.m); let m = self.m; let res = (self.n as u128) * (self.n as u128) % (m as u128); Self { n: res as u64, m } } fn powi(&self, mut b: u64) -> Self { let mut buf = self.clone(); let mut res = if (b & 1) == 0 { Self::one(self.m) } else { self.clone() }; b >>= 1; while b > 0 { buf = buf.sq(); if (b & 1) == 1 { res = res.mul(&buf); } b >>= 1; } res } }