結果
| 問題 |
No.577 Prime Powerful Numbers
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-10-14 18:55:01 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,977 bytes |
| コンパイル時間 | 1,673 ms |
| コンパイル使用メモリ | 169,512 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-17 18:44:49 |
| 合計ジャッジ時間 | 2,127 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 2 WA * 8 |
ソースコード
#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);
}
}
bool isPrime(const ll n)
{
constexpr int TEST = 30;
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;
}
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) {
cout << "No" << endl;
} else if (N == 2) {
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;
}
const ll rt = (ll)pow(res, 1.0 / j);
if (power(rt, j, (ll)1e18) == res) {
if (isPrime(rt)) {
ok = true;
break;
}
}
}
}
if (ok) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
}
return 0;
}