結果
| 問題 |
No.577 Prime Powerful Numbers
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-10-13 22:39:10 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,260 bytes |
| コンパイル時間 | 730 ms |
| コンパイル使用メモリ | 78,644 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-11-17 17:57:07 |
| 合計ジャッジ時間 | 1,302 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 8 WA * 2 |
ソースコード
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
// n is even -> yes
// n is odd -> p is even and q is odd
bool sieve[1000000];
vector<long long> primes;
bool is_prime(unsigned long long n) {
using i64 = long long;
using u64 = unsigned long long;
using f80 = long double;
if (n == 1) return false;
if (n == 2) return true;
int e = 0;
u64 s = n - 1;
while (s % 2 == 0) {
e++;
s /= 2;
}
auto modmul = [](u64 a, u64 b, u64 m) -> u64 {
if (a == 0 || b == 0) return 0;
if (a == 1) return b;
if (b == 1) return a;
u64 q = u64(f80(a) * b / m + 0.5);
u64 r = a * b - q * m;
return i64(r) < 0 ? r + m : r;
};
auto modpow = [modmul](u64 a, u64 b, u64 m) {
u64 ret = 1;
for (; b > 0; b >>= 1) {
if (b & 1) ret = modmul(ret, a, m);
a = modmul(a, a, m);
}
return ret;
};
auto check = [&](u64 a) {
u64 x = modpow(a, s, n);
if (x == 1) return true;
for (int i = 0; i < e; i++) {
if (x == n - 1) return true;
x = modmul(x, x, n);
}
return false;
};
for (int i : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
if (n % i != 0 && !check(i)) return false;
}
return true;
}
bool is_square(long long n) {
long long s = sqrt(n);
return s * s == n;
}
long long mul(long long a, long long b) {
if (a == 0) return 0;
if (b == 0) return 0;
if (a * b / b != a) return 1.1e18;
return a * b;
}
bool judge(long long n) {
if (n <= 3) return false;
if (n % 2 == 0) return true;
for (int i = 1; n - (1LL << i) >= 3; i++) {
long long p = n - (1LL << i);
if (is_prime(p)) return true;
if (is_square(p) && is_prime(sqrt(p))) return true;
for (long long q : primes) {
for (long long t = q*q*q; t <= p; t = mul(t, q)) {
if (t == p) return true;
}
}
}
return false;
}
int main() {
fill(sieve, sieve + 1000000, true);
sieve[0] = sieve[1] = false;
for (int i = 2; i < 1000000; i++) {
if (sieve[i]) {
primes.push_back(i);
for (int j = i * 2; j < 1000000; j++) {
sieve[j] = false;
}
}
}
int q;
cin >> q;
while (q--) {
long long n;
cin >> n;
cout << (judge(n) ? "Yes" : "No") << endl;
}
}