#[macro_export] macro_rules! read_line { ($($xs: tt)*) => { let mut buf = String::new(); std::io::stdin().read_line(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); expand!(iter, $($xs)*); }; } #[macro_export] macro_rules! expand { ($iter: expr,) => {}; ($iter: expr, mut $var: ident : $type: tt, $($xs: tt)*) => { let mut $var = value!($iter, $type); expand!($iter, $($xs)*); }; ($iter: expr, $var: ident : $type: tt, $($xs: tt)*) => { let $var = value!($iter, $type); expand!($iter, $($xs)*); }; } #[macro_export] macro_rules! value { ($iter:expr, ($($type: tt),*)) => { ($(value!($iter, $type)),*) }; ($iter: expr, [$type: tt; $len: expr]) => { (0..$len).map(|_| value!($iter, $type)).collect::>() }; ($iter: expr, Chars) => { value!($iter, String).unwrap().chars().collect::>() }; ($iter: expr, $type: ty) => { if let Some(v) = $iter.next() { v.parse::<$type>().ok() } else { None } }; } /// Returns: /// if n is prime number: /// true /// else: /// false pub fn is_prime(n: usize) -> bool { /// Returns: /// $a * b$ modulo $n$ pub fn mod_mul(a: usize, b: usize, m: usize) -> usize { ((a as u128) * (b as u128) % (m as u128)) as usize } /// Returns: /// $a^{n}$ modulo $m$ pub fn mod_pow(a: usize, mut n: usize, m: usize) -> usize { let mut res = 1; let mut x = a; while n > 0 { if n % 2 == 1 { res = mod_mul(res, x, m); } x = mod_mul(x, x, m); n /= 2; } res } if n == 0 || n == 1 || (n > 2 && n % 2 == 0) { return false; } if n == 2 { return true; } let s = (n - 1).trailing_zeros(); let d = (n - 1) / (1 << s); let f = |mut a| { a %= n; if a == 0 { return true; } let mut t = mod_pow(a, d, n); if t == 1 { return true; } if t == n - 1 { return true; } for _ in 1..s { t = mod_mul(t, t, n); if t == n - 1 { return true; } } false }; vec![2, 325, 9375, 28178, 450775, 9780504, 1795265022] .into_iter() .all(f) } fn main() { read_line!(n: usize,); let n = n.unwrap(); for _ in 0..n { read_line!(x: usize,); let x = x.unwrap(); println!("{} {}", x, if is_prime(x) { 1 } else { 0 }); } }