結果
問題 | No.577 Prime Powerful Numbers |
ユーザー | Pachicobue |
提出日時 | 2017-10-14 19:15:17 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,356 ms / 2,000 ms |
コード長 | 3,873 bytes |
コンパイル時間 | 1,725 ms |
コンパイル使用メモリ | 171,920 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-17 18:45:17 |
合計ジャッジ時間 | 6,365 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
5,248 KB |
testcase_01 | AC | 11 ms
5,248 KB |
testcase_02 | AC | 25 ms
5,248 KB |
testcase_03 | AC | 633 ms
5,248 KB |
testcase_04 | AC | 80 ms
5,248 KB |
testcase_05 | AC | 197 ms
5,248 KB |
testcase_06 | AC | 1,275 ms
5,248 KB |
testcase_07 | AC | 1,356 ms
5,248 KB |
testcase_08 | AC | 588 ms
5,248 KB |
testcase_09 | AC | 413 ms
5,248 KB |
testcase_10 | AC | 2 ms
5,248 KB |
ソースコード
#include <bits/stdc++.h> #define show(x) cerr << #x << " = " << x << endl using namespace std; using ll = long long; using pii = pair<int, int>; using vi = vector<int>; template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { os << "sz=" << v.size() << "\n["; for (const auto& p : v) { os << p << ","; } os << "]\n"; return os; } template <typename S, typename T> ostream& operator<<(ostream& os, const pair<S, T>& p) { os << "(" << p.first << "," << p.second << ")"; return os; } constexpr ll MOD = 1e9 + 7; template <typename T> constexpr T INF = numeric_limits<T>::max() / 100; mt19937 mt{random_device{}()}; ll power(const ll p, const ll n, const ll mod) { if (n == 0) { return 1; } if (n % 2 == 1) { return (ll)((__int128_t)((__int128_t)power(p, n - 1, mod) * (__int128_t)p) % mod); } else { const __int128_t pp = power(p, n / 2, mod); return (ll)((__int128_t)(pp * pp) % mod); } } __int128_t power(const ll p, const ll n) { if (n == 0) { return 1; } if (n % 2 == 1) { return ((__int128_t)((__int128_t)power(p, n - 1) * (__int128_t)p)); } else { const __int128_t pp = power(p, n / 2); return ((__int128_t)(pp * pp)); } } bool isPrime(const ll n) { constexpr int TEST = 1000; ll s = 0; ll d = n - 1; while (d % 2 == 0) { s++; d /= 2; } uniform_int_distribution<ll> dist{1, n - 1}; for (int i = 0; i < TEST; i++) { const ll a = dist(mt); if (power(a, n - 1, n) != 1) { return false; } ll num = power(a, d, n); if (num != 1) { bool comp = true; for (ll r = 0; r < s; r++) { if (num == n - 1) { comp = false; break; } num = (ll)((__int128_t)((__int128_t)num * (__int128_t)num) % n); } if (comp) { return false; } } } return true; } ll root(const ll p, const int n, bool& rootable) { rootable = false; ll inf = 1; ll sup = 2 * (ll)pow(p, 1.0 / n); while (inf < sup) { const ll mid = (inf + sup) / 2; const __int128_t calc = power(mid, n); if (calc == p) { rootable = true; return mid; } else if (calc < p) { if (inf == mid) { break; } inf = mid; } else { sup = mid; } } return inf; } int main() { cin.tie(0); ios::sync_with_stdio(false); int Q; cin >> Q; for (int q = 0; q < Q; q++) { ll N; cin >> N; if (N == 1) { show(N); cout << "No" << endl; } else if (N == 2) { show(N); cout << "No" << endl; } else if (N % 2 == 0) { cout << "Yes" << endl; } else { bool ok = false; for (int i = 1; (1LL << i) < N; i++) { if (ok) { break; } const ll res = N - (1LL << i); for (int j = 1; j <= (int)log2(res * 2 - 1); j++) { if (ok) { break; } bool rootable = false; const ll rt = root(res, j, rootable); if (rootable) { if (isPrime(rt)) { ok = true; break; } } } } if (ok) { cout << "Yes" << endl; } else { show(N); cout << "No" << endl; } } } return 0; }