結果
問題 | No.3030 ミラー・ラビン素数判定法のテスト |
ユーザー | 👑 Mizar |
提出日時 | 2022-08-21 18:15:42 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 26 ms / 9,973 ms |
コード長 | 20,218 bytes |
コンパイル時間 | 13,496 ms |
コンパイル使用メモリ | 387,764 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-16 23:53:12 |
合計ジャッジ時間 | 13,469 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 1 ms
5,248 KB |
testcase_04 | AC | 17 ms
5,248 KB |
testcase_05 | AC | 15 ms
5,248 KB |
testcase_06 | AC | 8 ms
5,248 KB |
testcase_07 | AC | 8 ms
5,248 KB |
testcase_08 | AC | 8 ms
5,248 KB |
testcase_09 | AC | 26 ms
5,248 KB |
ソースコード
// -*- coding:utf-8-unix -*- use std::io::{BufRead,Write}; // https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 #[allow(unused)] 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)*} }; } #[allow(unused)] 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)*} }; } #[allow(unused)] 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: std::cmp::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; } } } // Integer Wrapper Traits pub trait Zero { fn zero() -> Self; fn is_zero(&self) -> bool; } pub trait One { fn one() -> Self; } pub trait TrailingZeros { fn trailing_zeros(&self) -> u32; } pub trait WrappingAdd { fn wrapping_add(&self, v: &Self) -> Self; } pub trait WrappingSub { fn wrapping_sub(&self, v: &Self) -> Self; } pub trait WrappingMul { fn wrapping_mul(&self, v: &Self) -> Self; } pub trait WrappingNeg { fn wrapping_neg(&self) -> Self; } pub trait IntNum: Copy + Eq + Ord + Zero + One + TrailingZeros + WrappingAdd + WrappingSub + WrappingMul + WrappingNeg + std::marker::Sized + std::ops::Add<Output = Self> + std::ops::AddAssign + std::ops::Sub<Output = Self> + std::ops::SubAssign + std::ops::Mul<Output = Self> + std::ops::Div<Output = Self> + std::ops::Rem<Output = Self> + std::ops::BitAnd<Output = Self> + std::ops::Shl<u32> + std::ops::ShlAssign<u32> + std::ops::Shr<u32> + std::ops::ShrAssign<u32> + std::fmt::Debug { } pub trait UIntNum: IntNum {} pub trait IIntNum: IntNum {} macro_rules! define_zero_one { ($ty:ty, $zero:expr, $one:expr) => { impl Zero for $ty { #[inline] fn zero() -> Self { $zero } #[inline] fn is_zero(&self) -> bool { *self == $zero } } impl One for $ty { #[inline] fn one() -> Self { $one } } }; } define_zero_one!(usize, 0, 1); define_zero_one!(u8, 0, 1); define_zero_one!(u16, 0, 1); define_zero_one!(u32, 0, 1); define_zero_one!(u64, 0, 1); define_zero_one!(u128, 0, 1); define_zero_one!(isize, 0, 1); define_zero_one!(i8, 0, 1); define_zero_one!(i16, 0, 1); define_zero_one!(i32, 0, 1); define_zero_one!(i64, 0, 1); define_zero_one!(i128, 0, 1); define_zero_one!(f32, 0.0, 1.0); define_zero_one!(f64, 0.0, 1.0); macro_rules! integer_bitcount_impl { ($trait_name:ident, $method:ident, $t:ty) => { impl $trait_name for $t { #[inline] fn $method(&self) -> u32 { <$t>::$method(*self) } } }; } macro_rules! integer_wrapping_impl { ($trait_name:ident, $method:ident, $t:ty) => { impl $trait_name for $t { #[inline] fn $method(&self, v: &Self) -> Self { <$t>::$method(*self, *v) } } }; ($trait_name:ident, $method:ident, $t:ty, $rhs:ty) => { impl $trait_name<$rhs> for $t { #[inline] fn $method(&self, v: &$rhs) -> Self { <$t>::$method(*self, *v) } } }; } macro_rules! integer_wrapping_impl1 { ($trait_name:ident, $method:ident, $t:ty) => { impl $trait_name for $t { #[inline] fn $method(&self) -> Self { <$t>::$method(*self) } } }; } integer_bitcount_impl!(TrailingZeros, trailing_zeros, u8); integer_bitcount_impl!(TrailingZeros, trailing_zeros, u16); integer_bitcount_impl!(TrailingZeros, trailing_zeros, u32); integer_bitcount_impl!(TrailingZeros, trailing_zeros, u64); integer_bitcount_impl!(TrailingZeros, trailing_zeros, u128); integer_bitcount_impl!(TrailingZeros, trailing_zeros, usize); integer_bitcount_impl!(TrailingZeros, trailing_zeros, i8); integer_bitcount_impl!(TrailingZeros, trailing_zeros, i16); integer_bitcount_impl!(TrailingZeros, trailing_zeros, i32); integer_bitcount_impl!(TrailingZeros, trailing_zeros, i64); integer_bitcount_impl!(TrailingZeros, trailing_zeros, i128); integer_bitcount_impl!(TrailingZeros, trailing_zeros, isize); integer_wrapping_impl!(WrappingAdd, wrapping_add, u8); integer_wrapping_impl!(WrappingAdd, wrapping_add, u16); integer_wrapping_impl!(WrappingAdd, wrapping_add, u32); integer_wrapping_impl!(WrappingAdd, wrapping_add, u64); integer_wrapping_impl!(WrappingAdd, wrapping_add, u128); integer_wrapping_impl!(WrappingAdd, wrapping_add, usize); integer_wrapping_impl!(WrappingAdd, wrapping_add, i8); integer_wrapping_impl!(WrappingAdd, wrapping_add, i16); integer_wrapping_impl!(WrappingAdd, wrapping_add, i32); integer_wrapping_impl!(WrappingAdd, wrapping_add, i64); integer_wrapping_impl!(WrappingAdd, wrapping_add, i128); integer_wrapping_impl!(WrappingAdd, wrapping_add, isize); integer_wrapping_impl!(WrappingSub, wrapping_sub, u8); integer_wrapping_impl!(WrappingSub, wrapping_sub, u16); integer_wrapping_impl!(WrappingSub, wrapping_sub, u32); integer_wrapping_impl!(WrappingSub, wrapping_sub, u64); integer_wrapping_impl!(WrappingSub, wrapping_sub, u128); integer_wrapping_impl!(WrappingSub, wrapping_sub, usize); integer_wrapping_impl!(WrappingSub, wrapping_sub, i8); integer_wrapping_impl!(WrappingSub, wrapping_sub, i16); integer_wrapping_impl!(WrappingSub, wrapping_sub, i32); integer_wrapping_impl!(WrappingSub, wrapping_sub, i64); integer_wrapping_impl!(WrappingSub, wrapping_sub, i128); integer_wrapping_impl!(WrappingSub, wrapping_sub, isize); integer_wrapping_impl!(WrappingMul, wrapping_mul, u8); integer_wrapping_impl!(WrappingMul, wrapping_mul, u16); integer_wrapping_impl!(WrappingMul, wrapping_mul, u32); integer_wrapping_impl!(WrappingMul, wrapping_mul, u64); integer_wrapping_impl!(WrappingMul, wrapping_mul, u128); integer_wrapping_impl!(WrappingMul, wrapping_mul, usize); integer_wrapping_impl!(WrappingMul, wrapping_mul, i8); integer_wrapping_impl!(WrappingMul, wrapping_mul, i16); integer_wrapping_impl!(WrappingMul, wrapping_mul, i32); integer_wrapping_impl!(WrappingMul, wrapping_mul, i64); integer_wrapping_impl!(WrappingMul, wrapping_mul, i128); integer_wrapping_impl!(WrappingMul, wrapping_mul, isize); integer_wrapping_impl1!(WrappingNeg, wrapping_neg, u8); integer_wrapping_impl1!(WrappingNeg, wrapping_neg, u16); integer_wrapping_impl1!(WrappingNeg, wrapping_neg, u32); integer_wrapping_impl1!(WrappingNeg, wrapping_neg, u64); integer_wrapping_impl1!(WrappingNeg, wrapping_neg, u128); integer_wrapping_impl1!(WrappingNeg, wrapping_neg, usize); integer_wrapping_impl1!(WrappingNeg, wrapping_neg, i8); integer_wrapping_impl1!(WrappingNeg, wrapping_neg, i16); integer_wrapping_impl1!(WrappingNeg, wrapping_neg, i32); integer_wrapping_impl1!(WrappingNeg, wrapping_neg, i64); integer_wrapping_impl1!(WrappingNeg, wrapping_neg, i128); integer_wrapping_impl1!(WrappingNeg, wrapping_neg, isize); impl IntNum for usize {} impl IntNum for u8 {} impl IntNum for u16 {} impl IntNum for u32 {} impl IntNum for u64 {} impl IntNum for u128 {} impl IntNum for isize {} impl IntNum for i8 {} impl IntNum for i16 {} impl IntNum for i32 {} impl IntNum for i64 {} impl IntNum for i128 {} impl UIntNum for usize {} impl UIntNum for u8 {} impl UIntNum for u16 {} impl UIntNum for u32 {} impl UIntNum for u64 {} impl UIntNum for u128 {} impl IIntNum for isize {} impl IIntNum for i8 {} impl IIntNum for i16 {} impl IIntNum for i32 {} impl IIntNum for i64 {} impl IIntNum for i128 {} // モンゴメリ剰余乗算 (Montgomery modular multiplication) pub trait UMontTrait<T: UIntNum> { fn m(&self) -> T; fn mi(&self) -> T; fn tr(&self) -> T; fn tr2(&self) -> T; #[inline] fn d(&self) -> T { let mut d = self.m() - T::one(); let k = d.trailing_zeros(); d >>= k; d } #[inline] fn k(&self) -> u32 { let d = self.m() - T::one(); let k = d.trailing_zeros(); k } fn new(m: T) -> Self; fn mrmul(&self, ar: T, br: T) -> T; fn mr(&self, ar: T) -> T; #[inline] fn ar(&self, a: T) -> T { // == a * r (mod m) debug_assert!(a < self.m()); self.mrmul(a, self.tr2()) } #[inline] fn powir(&self, mut ar: T, mut b: T) -> T { // == ((ar / r) ** b) * r (mod m) debug_assert!(ar < self.m()); let mut t = if (b & T::one()).is_zero() { self.tr() } else { ar }; b >>= 1; while !b.is_zero() { ar = self.mrmul(ar, ar); t = self.mrmul(t, if (b & T::one()).is_zero() { self.tr() } else { ar }); b >>= 1; } t } fn prime_test_once(&self, base: T) -> bool { // Miller-Rabin primality test debug_assert!(base > T::one()); let (m, r, d, k) = (self.m(), self.tr(), self.d(), self.k()); let b = base % m; if b.is_zero() { return true; } let mut br = self.powir(self.ar(b), d); if br == r { return true; } let negr = m - r; for _ in 0..k { if br == negr { return true; } br = self.mrmul(br, br); } false } } pub struct U64Mont { m: u64, // m is odd, and m > 2 mi: u64, // m * mi == 1 (mod 2**64) r: u64, // == 2**64 (mod m) r2: u64, // == 2**128 (mod m) d: u64, // == (m - 1) >> (m - 1).trailing_zeros() k: u32, // == (m - 1).trailing_zeros() } impl UMontTrait<u64> for U64Mont { #[inline] fn m(&self) -> u64 { self.m } #[inline] fn mi(&self) -> u64 { self.mi } #[inline] fn tr(&self) -> u64 { self.r } #[inline] fn tr2(&self) -> u64 { self.r2 } #[inline] fn d(&self) -> u64 { self.d } #[inline] fn k(&self) -> u32 { self.k } #[inline] fn new(m: u64) -> Self { debug_assert_eq!(m & 1, 1); // // m is odd number, m = 2*k+1, m >= 1, m < 2**64, k is non-negative integer, k >= 0, k < 2**63 // mi0 := m; // = 2*k+1 = (1+(2**2)*((k*(k+1))**1))/(2*k+1) let mut mi = m; // mi1 := mi0 * (2 - (m * mi0)); // = (1-(2**4)*((k*(k+1))**2))/(2*k+1) // mi2 := mi1 * (2 - (m * mi1)); // = (1-(2**8)*((k*(k+1))**4))/(2*k+1) // mi3 := mi2 * (2 - (m * mi2)); // = (1-(2**16)*((k*(k+1))**8))/(2*k+1) // mi4 := mi3 * (2 - (m * mi3)); // = (1-(2**32)*((k*(k+1))**16))/(2*k+1) // mi5 := mi4 * (2 - (m * mi4)); // = (1-(2**64)*((k*(k+1))**32))/(2*k+1) // // (m * mi5) mod 2**64 = ((2*k+1) * mi5) mod 2**64 = 1 mod 2**64 for _ in 0..5 { mi = mi.wrapping_mul(2u64.wrapping_sub(m.wrapping_mul(mi))); } debug_assert_eq!(m.wrapping_mul(mi), 1); // m * mi == 1 (mod 2**64) let r: u64 = m.wrapping_neg() % m; // == 2**64 (mod m) let r2: u64 = ((m as u128).wrapping_neg() % (m as u128)) as u64; // == 2**128 (mod m) let mut d = m - 1; let k = d.trailing_zeros(); d >>= k; debug_assert_eq!(Self { m, mi, r, r2, d, k }.mr(r), 1); // r / r == 1 (mod m) debug_assert_eq!(Self { m, mi, r, r2, d, k }.mrmul(1, r2), r); // r2 / r == r (mod m) Self { m, mi, r, r2, d, k } } #[inline] fn mrmul(&self, ar: u64, br: u64) -> u64 { // == (ar * br) / r (mod m) debug_assert!(ar < self.m); debug_assert!(br < self.m); let (m, mi) = (self.m, self.mi); let t: u128 = (ar as u128) * (br as u128); let t: (u64, bool) = ((t >> 64) as u64).overflowing_sub((((((t as u64).wrapping_mul(mi)) as u128) * (m as u128)) >> 64) as u64); if t.1 { t.0.wrapping_add(m) } else { t.0 } } #[inline] fn mr(&self, ar: u64) -> u64 { // == ar / r (mod m) debug_assert!(ar < self.m); let (m, mi) = (self.m, self.mi); let t: (u64, bool) = (((((ar.wrapping_mul(mi)) as u128) * (m as u128)) >> 64) as u64).overflowing_neg(); if t.1 { t.0.wrapping_add(m) } else { t.0 } } } pub struct U32Mont { m: u32, // m is odd, and m > 2 mi: u32, // m * mi == 1 (mod 2**32) r: u32, // == 2**32 (mod m) r2: u32, // == 2**64 (mod m) d: u32, // == (m - 1) >> (m - 1).trailing_zeros() k: u32, // == (m - 1).trailing_zeros() } impl UMontTrait<u32> for U32Mont { #[inline] fn m(&self) -> u32 { self.m } #[inline] fn mi(&self) -> u32 { self.mi } #[inline] fn tr(&self) -> u32 { self.r } #[inline] fn tr2(&self) -> u32 { self.r2 } #[inline] fn d(&self) -> u32 { self.d } #[inline] fn k(&self) -> u32 { self.k } #[inline] fn new(m: u32) -> Self { debug_assert_eq!(m & 1, 1); // // m is odd number, m = 2*k+1, m >= 1, m < 2**64, k is non-negative integer, k >= 0, k < 2**63 // mi0 := m; // = 2*k+1 = (1+(2**2)*((k*(k+1))**1))/(2*k+1) let mut mi = m; // mi1 := mi0 * (2 - (m * mi0)); // = (1-(2**4)*((k*(k+1))**2))/(2*k+1) // mi2 := mi1 * (2 - (m * mi1)); // = (1-(2**8)*((k*(k+1))**4))/(2*k+1) // mi3 := mi2 * (2 - (m * mi2)); // = (1-(2**16)*((k*(k+1))**8))/(2*k+1) // mi4 := mi3 * (2 - (m * mi3)); // = (1-(2**32)*((k*(k+1))**16))/(2*k+1) // // (m * mi4) mod 2**32 = ((2*k+1) * mi4) mod 2**32 = 1 mod 2**32 for _ in 0..4 { mi = mi.wrapping_mul(2u32.wrapping_sub(m.wrapping_mul(mi))); } debug_assert_eq!(m.wrapping_mul(mi), 1); // m * mi == 1 (mod 2**32) let r: u32 = m.wrapping_neg() % m; // == 2**64 (mod m) let r2: u32 = ((m as u64).wrapping_neg() % (m as u64)) as u32; // == 2**64 (mod m) let mut d = m - 1; let k = d.trailing_zeros(); d >>= k; debug_assert_eq!(Self { m, mi, r, r2, d, k }.mr(r), 1); // r / r == 1 (mod m) debug_assert_eq!(Self { m, mi, r, r2, d, k }.mrmul(1, r2), r); // r2 / r == r (mod m) Self { m, mi, r, r2, d, k } } #[inline] fn mrmul(&self, ar: u32, br: u32) -> u32 { // == (ar * br) / r (mod m) debug_assert!(ar < self.m); debug_assert!(br < self.m); let (m, mi) = (self.m, self.mi); let t: u64 = (ar as u64) * (br as u64); let t: (u32, bool) = ((t >> 32) as u32).overflowing_sub((((((t as u32).wrapping_mul(mi)) as u64) * (m as u64)) >> 32) as u32); if t.1 { t.0.wrapping_add(m) } else { t.0 } } #[inline] fn mr(&self, ar: u32) -> u32 { // == ar / r (mod m) debug_assert!(ar < self.m); let (m, mi) = (self.m, self.mi); let t: (u32, bool) = (((((ar.wrapping_mul(mi)) as u64) * (m as u64)) >> 32) as u32).overflowing_neg(); if t.1 { t.0.wrapping_add(m) } else { t.0 } } } pub fn prime_test_32(m: u32) -> bool { if m == 2 { return true; } if m == 1 || (m & 1) == 0 { return false; } let u32mont = U32Mont::new(m); match m { // Deterministic variants of the Miller-Rabin primality test // http://miller-rabin.appspot.com/ 0..=49141 => { u32mont.prime_test_once(921211727) }, 0..=360018361 => { u32mont.prime_test_once(1143370) && u32mont.prime_test_once(2350307676) }, _ => { u32mont.prime_test_once(2) && u32mont.prime_test_once(7) && u32mont.prime_test_once(61) } } } pub fn prime_test_64(m: u64) -> bool { if m == 2 { return true; } if m == 1 || (m & 1) == 0 { return false; } let u64mont = U64Mont::new(m); match m { // Deterministic variants of the Miller-Rabin primality test // http://miller-rabin.appspot.com/ 0..=341531 => { u64mont.prime_test_once(9345883071009581737) }, 0..=1050535501 => { u64mont.prime_test_once(336781006125) && u64mont.prime_test_once(9639812373923155) }, 0..=350269456337 => { u64mont.prime_test_once(4230279247111683200) && u64mont.prime_test_once(14694767155120705706) && u64mont.prime_test_once(16641139526367750375) }, 0..=55245642489451 => { u64mont.prime_test_once(2) && u64mont.prime_test_once(141889084524735) && u64mont.prime_test_once(1199124725622454117) && u64mont.prime_test_once(11096072698276303650) }, 0..=7999252175582851 => { u64mont.prime_test_once(2) && u64mont.prime_test_once(4130806001517) && u64mont.prime_test_once(149795463772692060) && u64mont.prime_test_once(186635894390467037) && u64mont.prime_test_once(3967304179347715805) }, 0..=585226005592931977 => { u64mont.prime_test_once(2) && u64mont.prime_test_once(123635709730000) && u64mont.prime_test_once(9233062284813009) && u64mont.prime_test_once(43835965440333360) && u64mont.prime_test_once(761179012939631437) && u64mont.prime_test_once(1263739024124850375) }, _ => { u64mont.prime_test_once(2) && u64mont.prime_test_once(325) && u64mont.prime_test_once(9375) && u64mont.prime_test_once(28178) && u64mont.prime_test_once(450775) && u64mont.prime_test_once(9780504) && u64mont.prime_test_once(1795265022) }, } } pub fn prime_test_64_f(m: u64) -> bool { if m == 2 { return true; } if m == 1 || (m & 1) == 0 { return false; } let u64mont = U64Mont::new(m); u64mont.prime_test_once(2) && u64mont.prime_test_once(325) && u64mont.prime_test_once(9375) && u64mont.prime_test_once(28178) && u64mont.prime_test_once(450775) && u64mont.prime_test_once(9780504) && u64mont.prime_test_once(1795265022) } fn main() { let start_time = std::time::Instant::now(); let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););} /* input! { n: usize, x: [u64; n], } */ let input = std::io::stdin(); let mut lines = std::io::BufReader::new(input.lock()).lines(); let n: usize = lines.next().unwrap().unwrap().parse().unwrap(); /* let x: Vec<u64> = lines.take(n).map(|l| l.unwrap().parse().unwrap()).collect(); let elapsed1 = start_time.elapsed(); let res: Vec<bool> = x.iter().map(|&v| prime_test_64_f(v)).collect(); let elapsed2 = start_time.elapsed(); for i in 0..n { puts!("{} {}\n", x[i], if res[i] { "1" } else { "0" }); } let elapsed3 = start_time.elapsed(); out.flush().unwrap(); let elapsed4 = start_time.elapsed(); eprint!( " input: {}us\ncompute: {}us\n output: {}us\n wflush: {}us\n", elapsed1.as_micros(), elapsed2.as_micros(), elapsed3.as_micros(), elapsed4.as_micros(), ); */ for _ in 0..n { let x: u64 = lines.next().unwrap().unwrap().parse().unwrap(); puts!("{} {}\n", x, if prime_test_64_f(x) { "1" } else { "0" }); } let elapsed = start_time.elapsed(); eprint!("{}us\n", elapsed.as_micros()); }