#line 1 "main.test.cpp" #define PROBLEM "https://yukicoder.me/problems/no/3030" #include #line 1 "math/mod_pow.hpp" template U mod_pow(T base, T exp, T mod){ if(base==0)return 0; T ans = 1; base %= mod; while (exp > 0) { if (exp & 1) { ans *= base; ans %= mod; } base *= base; base %= mod; exp >>= 1; } return ans; } ///@brief mod pow(バイナリ法) #line 7 "main.test.cpp" using namespace std; namespace prime { namespace miller{ using i128 = __int128_t; using u128 = __uint128_t; using u64 = __uint64_t; bool miller_rabin(u64 n,const u64 bases[],int siz) { u64 d = n - 1; u64 q = __builtin_ctz(d); d >>= q; for(int i=0;i(a, d, n) != 1) { bool flag = true; for (u64 r = 0; r < q; r++) { u64 pow = mod_pow(a, d * (1ll << r), n); if (pow == n - 1) { flag = false; break; } } if (flag) { return false; } } } return true; } bool is_prime(u64 n){ static constexpr u64 bases_int[3] = {2, 7, 61}; // intだと、2,7,61で十分 static constexpr u64 bases_ll[7] = {2,325,9375,28178,450775,9780504,1795265022}; if (n < 2) { return false; } else if (n == 2) { return true; } else if (~n & 1) { return false; } if(n<(1ul<<31)){ return miller_rabin(n, bases_int, 3); } else { return miller_rabin(n, bases_ll, 7); } } }; }; using prime::miller::is_prime; ///@brief fast prime check(MillerRabinの素数判定法) int main(){ int n; cin >> n; while(n--){ long long x; cin>>x; cout << x << ' ' << is_prime(x) << '\n'; } }