結果
問題 | No.3030 ミラー・ラビン素数判定法のテスト |
ユーザー | AC2K |
提出日時 | 2023-03-02 21:23:26 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 7,912 bytes |
コンパイル時間 | 2,247 ms |
コンパイル使用メモリ | 203,840 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-09-17 14:43:04 |
合計ジャッジ時間 | 3,070 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
ソースコード
#line 2 "template.hpp" #include<bits/stdc++.h> using namespace std; #define rep(i, N) for(int i=0;i<(N);i++) #define all(x) (x).begin(),(x).end() #define popcount(x) __builtin_popcount(x) using i128=__int128_t; using ll = long long; using ld = long double; using graph = vector<vector<int>>; using P = pair<int, int>; constexpr int inf = 1e9; constexpr ll infl = 1e18; constexpr ld eps = 1e-6; constexpr long double pi = acos(-1); constexpr ll MOD = 1e9 + 7; constexpr ll MOD2 = 998244353; constexpr int dx[] = { 1,0,-1,0 }; constexpr int dy[] = { 0,1,0,-1 }; template<class T>inline void chmax(T&x,T y){if(x<y)x=y;} template<class T>inline void chmin(T&x,T y){if(x>y)x=y;} #line 2 "math/montgomery.hpp" class montgomery64 { using mint = montgomery64; using i64 = int64_t; using u64 = uint64_t; using u128 = __uint128_t; static u64 mod; static u64 r; static u64 n2; static u64 get_r() { u64 ret = mod; for (i64 i = 0; i < 5; ++i) ret *= 2 - mod * ret; return ret; } public: static void set_mod(const u128& m) { assert(m < (i128(1) << 64)); assert((m & 1) == 1); mod = m; n2 = -u128(m) % m; r = get_r(); assert(r * mod == 1); } protected: i128 a; public: montgomery64() : a(0) {} template<typename T> montgomery64(const T& b) : a(reduce((u128(b) + mod)* n2)) {}; private: template<class T> static u64 reduce(const T& b) { return (b + u128(u64(b) * u64(-r)) * mod) >> 64; } public: template<class T> mint& operator=(const T& rhs) { return (*this) = mint(rhs); } mint& operator+=(const mint& b) { if (i64(a += b.a - 2 * mod) < 0) a += 2 * mod; return *this; } mint& operator-=(const mint& b) { if (i64(a -= b.a) < 0) a += 2 * mod; return *this; } mint& operator*=(const mint& b) { a = reduce(u128(a) * b.a); return *this; } mint& operator/=(const mint& b) { *this *= b.inv(); return *this; } mint operator+(const mint& b) const { return mint(*this) += b; } mint operator-(const mint& b) const { return mint(*this) -= b; } mint operator*(const mint& b) const { return mint(*this) *= b; } mint operator/(const mint& b) const { return mint(*this) /= b; } bool operator==(const mint& b) const { return (a >= mod ? a - mod : a) == (b.a >= mod ? b.a - mod : b.a); } bool operator!=(const mint& b) const { return (a >= mod ? a - mod : a) != (b.a >= mod ? b.a - mod : b.a); } mint operator-() const { return mint() - mint(*this); } mint pow(u128 n) const { mint ret(1), mul(*this); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream& operator<<(ostream& os, const mint& b) { return os << b.val(); } friend istream& operator>>(istream& is, mint& b) { int64_t t; is >> t; b = montgomery64(t); return (is); } mint inv() const { return pow(mod - 2); } u64 val() const { u64 ret = reduce(a); return ret >= mod ? ret - mod : ret; } static u64 get_mod() { return mod; } }; typename montgomery64::u64 montgomery64::mod, montgomery64::r, montgomery64::n2; class ArbitraryModInt { using mint = ArbitraryModInt; using i32 = int32_t; using u32 = uint32_t; using u64 = uint64_t; static u32 mod; static u32 r; static u32 n2; static u32 get_r() { u32 ret = mod; for (i32 i = 0; i < 4; ++i) ret *= 2 - mod * ret; return ret; } public: static void set_mod(u32 m) { assert(m < (1 << 30)); assert((m & 1) == 1); mod = m; n2 = -u64(m) % m; r = get_r(); assert(r * mod == 1); } protected: u32 a; public: ArbitraryModInt() : a(0) {} ArbitraryModInt(const int64_t& b) : a(reduce(u64(b% mod + mod)* n2)) {}; static u32 reduce(const u64& b) { return (b + u64(u32(b) * u32(-r)) * mod) >> 32; } mint& operator+=(const mint& b) { if (i32(a += b.a - 2 * mod) < 0) a += 2 * mod; return *this; } mint& operator-=(const mint& b) { if (i32(a -= b.a) < 0) a += 2 * mod; return *this; } mint& operator*=(const mint& b) { a = reduce(u64(a) * b.a); return *this; } mint& operator/=(const mint& b) { *this *= b.inverse(); return *this; } mint operator+(const mint& b) const { return mint(*this) += b; } mint operator-(const mint& b) const { return mint(*this) -= b; } mint operator*(const mint& b) const { return mint(*this) *= b; } mint operator/(const mint& b) const { return mint(*this) /= b; } bool operator==(const mint& b) const { return (a >= mod ? a - mod : a) == (b.a >= mod ? b.a - mod : b.a); } bool operator!=(const mint& b) const { return (a >= mod ? a - mod : a) != (b.a >= mod ? b.a - mod : b.a); } mint operator-() const { return mint() - mint(*this); } mint pow(u64 n) const { mint ret(1), mul(*this); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream& operator<<(ostream& os, const mint& b) { return os << b.get(); } friend istream& operator>>(istream& is, mint& b) { int64_t t; is >> t; b = ArbitraryModInt(t); return (is); } mint inverse() const { return pow(mod - 2); } u32 get() const { u32 ret = reduce(a); return ret >= mod ? ret - mod : ret; } static u32 get_mod() { return mod; } }; typename ArbitraryModInt::u32 ArbitraryModInt::mod; typename ArbitraryModInt::u32 ArbitraryModInt::r; typename ArbitraryModInt::u32 ArbitraryModInt::n2; /// @brief Montgomery ///by https://nyaannyaan.github.io/library/modint/modint-montgomery64.hpp,https://nyaannyaan.github.io/library/modint/arbitrary-prime-modint.hpp #line 3 "main.test.cpp" namespace fast_prime{ //fast_is_prime using u64 = uint64_t; namespace miller_rabin{ constexpr u64 bases_int[] = {2, 7, 67}; constexpr u64 bases_long[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}; template<typename mint> bool miller_rabin(u64 n, const u64 bases[],const int len){ if (mint::get_mod() != n){ mint::set_mod(n); } u64 d = n - 1; u64 q = __builtin_ctzll(d); d >>= q; const mint e1 = 1, e2 = n - 1; for (int i = 0; i < len; i++){ u64 a = bases[i]; if (n <= a){ break; } u64 t = d; mint y = mint(a).pow(t); while(t!=n-1&&y!=e1&&y!=e2){ y *= y; t <<= 1; } if (y != e2 && (~t & 1ul)){ return false; } } return true; } bool is_prime(u64 n){ if (n == 2){ return true; } else if (n < 2||(~n & 1)){ return false; } if (n < (1ul << 30)){ return miller_rabin<ArbitraryModInt>(n, bases_int, 3); } else{ return miller_rabin<montgomery64>(n, bases_long, 7); } } }; }; using fast_prime::miller_rabin::is_prime; int main(){ int n; cin >> n; while (n--){ unsigned long long x; cin>>x; cout << x << ' '; if(is_prime(x)){ cout << "1\n"; }else{ cout << "0\n"; } } }