結果
| 問題 | No.577 Prime Powerful Numbers |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-12-02 22:22:06 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,478 bytes |
| コンパイル時間 | 2,567 ms |
| コンパイル使用メモリ | 194,900 KB |
| 最終ジャッジ日時 | 2025-02-18 06:21:18 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 3 WA * 7 |
コンパイルメッセージ
main.cpp: In function ‘void solve(int)’:
main.cpp:145:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
145 | scanf("%lld", &n);
| ~~~~~^~~~~~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:165:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
165 | scanf("%d", &T);
| ~~~~~^~~~~~~~~~
ソースコード
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int MAXN = 1e6 + 10;
const int MM = 1e9 + 7;
namespace prime {
using uint128 = __uint128_t;
using uint64 = unsigned long long;
using int64 = long long;
using uint32 = unsigned int;
using pii = std::pair<uint64, uint32>;
inline uint64 sqr(uint64 x) { return x * x; }
inline uint32 isqrt(uint64 x) { return sqrtl(x); }
inline uint32 ctz(uint64 x) { return __builtin_ctzll(x); }
template <typename word>
word gcd(word a, word b) {
while (b) { word t = a % b; a = b; b = t; }
return a;
}
template <typename word, typename dword, typename sword>
struct Mod {
Mod(): x(0) {}
Mod(word _x): x(init(_x)) {}
bool operator == (const Mod& rhs) const { return x == rhs.x; }
bool operator != (const Mod& rhs) const { return x != rhs.x; }
Mod& operator += (const Mod& rhs) { if ((x += rhs.x) >= mod) x -= mod; return *this; }
Mod& operator -= (const Mod& rhs) { if (sword(x -= rhs.x) < 0) x += mod; return *this; }
Mod& operator *= (const Mod& rhs) { x = reduce(dword(x) * rhs.x); return *this; }
Mod operator + (const Mod &rhs) const { return Mod(*this) += rhs; }
Mod operator - (const Mod &rhs) const { return Mod(*this) -= rhs; }
Mod operator * (const Mod &rhs) const { return Mod(*this) *= rhs; }
Mod operator - () const { return Mod() - *this; }
Mod pow(uint64 e) const {
Mod ret(1);
for (Mod base = *this; e; e >>= 1, base *= base) {
if (e & 1) ret *= base;
}
return ret;
}
word get() const { return reduce(x); }
static constexpr int word_bits = sizeof(word) * 8;
static word modulus() { return mod; }
static word init(word w) { return reduce(dword(w) * r2); }
static void set_mod(word m) { mod = m, inv = mul_inv(mod), r2 = -dword(mod) % mod; }
static word reduce(dword x) {
word y = word(x >> word_bits) - word((dword(word(x) * inv) * mod) >> word_bits);
return sword(y) < 0 ? y + mod : y;
}
static word mul_inv(word n, int e = 6, word x = 1) {
return !e ? x : mul_inv(n, e - 1, x * (2 - x * n));
}
static word mod, inv, r2;
word x;
};
using Mod64 = Mod<uint64, uint128, int64>;
using Mod32 = Mod<uint32, uint64, int>;
template <> uint64 Mod64::mod = 0;
template <> uint64 Mod64::inv = 0;
template <> uint64 Mod64::r2 = 0;
template <> uint32 Mod32::mod = 0;
template <> uint32 Mod32::inv = 0;
template <> uint32 Mod32::r2 = 0;
template <class word, class mod>
bool composite(word n, const uint32* bases, int m) {
mod::set_mod(n);
int s = __builtin_ctzll(n - 1);
word d = (n - 1) >> s;
mod one{1}, minus_one{n - 1};
for (int i = 0, j; i < m; ++i) {
mod a = mod(bases[i]).pow(d);
if (a == one || a == minus_one) continue;
for (j = s - 1; j > 0; --j) {
if ((a *= a) == minus_one) break;
}
if (j == 0) return true;
}
return false;
}
bool is_prime(uint64 n) {
assert(n < (uint64(1) << 63));
static const uint32 bases[][7] = {
{2, 3},
{2, 299417},
{2, 7, 61},
{15, 176006322, uint32(4221622697)},
{2, 2570940, 211991001, uint32(3749873356)},
{2, 2570940, 880937, 610386380, uint32(4130785767)},
{2, 325, 9375, 28178, 450775, 9780504, 1795265022}
};
if (n <= 1) return false;
if (!(n & 1)) return n == 2;
if (n <= 8) return true;
int x = 6, y = 7;
if (n < 1373653) x = 0, y = 2;
else if (n < 19471033) x = 1, y = 2;
else if (n < 4759123141) x = 2, y = 3;
else if (n < 154639673381) x = y = 3;
else if (n < 47636622961201) x = y = 4;
else if (n < 3770579582154547) x = y = 5;
if (n < (uint32(1) << 31)) {
return !composite<uint32, Mod32>(n, bases[x], y);
} else if (n < (uint64(1) << 63)) {
return !composite<uint64, Mod64>(n, bases[x], y);
}
return true;
}
} // namespace prime
ll pw(ll p, ll q) {
ll ret = 1;
for (; q; q >>= 1) {
if (q & 1) {
ret = ret * p;
}
p = p * p;
}
return ret;
}
bool check(ll x) {
for (int b = 1; b <= 64; ++b) {
ll q = exp(log(x + 0.5) / b);
if (prime::is_prime(q) && pw(q, b) == x) {
return true;
}
}
return false;
}
void solve(int casi) {
ll n;
scanf("%lld", &n);
if (n <= 3) {
puts("No");
return ;
}
if (n % 2 == 0) {
puts("Yes");
return ;
}
for (ll pw2 = 2; pw2 < n; pw2 *= 2) {
if (check(n - pw2)) {
puts("Yes");
return ;
}
}
puts("No");
}
int main(){
int T = 1;
scanf("%d", &T);
for (int i = 1; i <= T; i++)
solve(i);
return 0;
}