static A: [i32; 3] = [2, 7, 61]; static B: [i64; 7] = [2, 325, 9375, 28178, 450775, 9780504, 1795265022]; fn miller_rabin_i64(n: i64) -> bool { let mut d = n - 1; while d & 1 == 0 { d /= 2; } let (e, rev) = (1, n as i128 - 1); for b in B.into_iter() { if n <= *b { break; } let mut t = d as i128; let mut y = modpow(*b as i128, t as i128, n as i128); while t != n as i128 - 1 && y != e && y != rev { y *= y; y %= n as i128; t *= 2; t %= n as i128; } if y != rev && t % 2 == 0 { return false; } } return true; } fn miller_rabin_i32(n: i32) -> bool { let mut d = n - 1; while d & 1 == 0 { d /= 2; } let (e, rev) = (1, n as i128 - 1); for a in A.into_iter() { if n <= *a { break; } let mut t = d as i128; let mut y = modpow(*a as i128, t as i128, n as i128); while t as i128 != n as i128 - 1 && y != e && y != rev { y *= y; y %= n as i128; t *= 2; t %= n as i128; } if y != rev && t & 1 == 0 { return false; } } true } fn modpow(_n: i128, _t: i128, modulo: i128) -> i128 { let mut ret = 1i128; let mut n = _n; let mut t = _t; while t != 0 { if t & 1 == 1 { ret *= n; } ret %= modulo; t >>= 1; n *= n; n %= modulo; } ret } fn is_prime(n: i64) -> bool { if n == 2 { return true; } else if n == 1 || n & 1 == 0 { return false; } else if n < (1i64 << 31) { return miller_rabin_i32(n as i32); } miller_rabin_i64(n) } #[allow(dead_code)] fn solve(read: &mut snio::Reader>) { let n = read.i64(); for _ in 0..n { let a = read.i64(); println!("{} {}",a ,is_prime(a) as u8); } } //use proconio::input; fn main() { let t = std::io::stdin(); let mut read = snio::Reader::new(t.lock()); let n = 1; for _ in 0..n { solve(&mut read); } } #[allow(dead_code)] pub mod snio { pub struct Reader { reader: R, buf: std::collections::VecDeque, } impl Reader { pub fn new(reader: R) -> Self { Self { reader, buf: std::collections::VecDeque::new(), } } fn load(&mut self) { while self.buf.is_empty() { let mut s = String::new(); let length = self.reader.read_line(&mut s).unwrap(); if length == 0 { break; } self.buf.extend(s.split_whitespace().map(|s| s.to_owned())); } } pub fn string(&mut self) -> String { self.load(); self.buf.pop_front().unwrap_or_else(|| panic!("input ended")) } pub fn char(&mut self) -> char { let string = self.string(); let mut chars = string.chars(); let res = chars.next().unwrap(); assert!(chars.next().is_none(), "invalid input!"); res } pub fn chars(&mut self) -> Vec { self.read::().chars().collect() } pub fn read(&mut self) -> T where ::Err: ::std::fmt::Debug, { self.string().parse::().expect("Failed to parse the input.") } } macro_rules! definition_of_reader_of_numbers { ($($ty:tt,)*) => { impl Reader { $( #[inline] pub fn $ty (&mut self) -> $ty { self.read::<$ty>() } )* } } } definition_of_reader_of_numbers! { u8,u16,u32,u64,usize, i8,i16,i32,i64,isize, f32,f64, } } const INF:i64 = 1i64 << 60;