結果
問題 | No.3030 ミラー・ラビン素数判定法のテスト |
ユーザー | tarakojo1019 |
提出日時 | 2021-03-26 20:42:57 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,972 bytes |
コンパイル時間 | 1,012 ms |
コンパイル使用メモリ | 114,240 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-05-06 13:39:55 |
合計ジャッジ時間 | 2,865 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | WA | - |
testcase_05 | AC | 138 ms
5,376 KB |
testcase_06 | AC | 65 ms
5,376 KB |
testcase_07 | AC | 68 ms
5,376 KB |
testcase_08 | AC | 67 ms
5,376 KB |
testcase_09 | AC | 245 ms
5,376 KB |
ソースコード
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> //#define ENVIRONMENT_LINKED_ACL #ifdef ENVIRONMENT_LINKED_ACL #include <atcoder/convolution> #include <atcoder/lazysegtree> #include <atcoder/segtree.hpp> #endif //#define ENVIRONMENT_LINKED_BOOST #ifdef ENVIRONMENT_LINKED_BOOST #include <boost/multiprecision/cpp_dec_float.hpp> #include <boost/multiprecision/cpp_int.hpp> #endif using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; #define REP(i, a, b) for (ll i = a; i < b; ++i) #define REPREV(i, a, b) for (ll i = a; i > b; --i) const int _ = []() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(10); return 0; }(); template <typename value_t> void resize(value_t& v, const value_t&& val) { v = val; } template <typename vec_t, typename value_t, typename... arg_t> void resize(std::vector<vec_t>& v, const value_t&& val, int size, arg_t... arg) { v.resize(size); for (auto& c : v) resize(c, std::forward<const value_t>(val), arg...); } template <typename A, typename B> void chmin(A& a, const B& b) { a = min(a, static_cast<A>(b)); }; template <typename A, typename B> void chmax(A& a, const B& b) { a = max(a, static_cast<A>(b)); }; template <typename T> T modpow(T r, T n, T mod) { T ans = 1; T tmp = r % mod; while (n > 0) { if ((n & 1) > 0) { ans = ans * tmp % mod; } tmp = tmp * tmp % mod; n >>= 1; } return ans; } //素数判定O(logn) bool is_prime(unsigned int n) { if (n == 1 || n % 2 == 0) return n == 2; unsigned int d = n - 1, s = 0; while (d % 2 == 0) { d /= 2; ++s; } constexpr std::array<unsigned int, 3> test = {2, 7, 61}; for (auto a : test) { unsigned long long tmp = modpow<unsigned long long>(a, d, n); if (tmp == 1) continue; bool no_prime = true; for (int r = 0; r < s; ++r) { if (tmp == n - 1) { no_prime = false; break; } tmp = tmp * tmp % n; } if (no_prime) return false; } return true; } unsigned long long mod_mul(unsigned long long a, unsigned long long b, unsigned long long mod) { long long ret = a * b - mod * (unsigned long long)((long double)(a) * (long double)(b) / (long double)(mod)); return ret + mod * (ret < 0) - mod * (ret >= (long long)mod); } unsigned long long mod_pow(unsigned long long x, unsigned long long k, unsigned long long mod) { unsigned long long res = 1; while (k) { if (k & 1) res = mod_mul(res, x, mod); x = mod_mul(x, x, mod); k >>= 1; } return res; } bool is_prime_ull(unsigned long long n) { if (n == 1 || n % 2 == 0) return n == 2; unsigned long long d = n - 1, s = 0; while (d % 2 == 0) { d /= 2; ++s; } constexpr std::array<unsigned long long, 7> test = {2, 325, 9375, 28178, 450775, 9780504, 1795265022ULL}; for (auto a : test) { __uint128_t tmp = modpow<__uint128_t>(a, d, n); if (tmp == 1) continue; bool no_prime = true; for (int r = 0; r < s; ++r) { if (tmp == n - 1) { no_prime = false; break; } tmp = tmp * tmp % n; } if (no_prime) return false; } return true; } int main() { ll n; cin >> n; REP(i, 0, n) { ull x; cin >> x; cout << x << " " << (is_prime_ull(x) ? 1 : 0) << endl; } return 0; }