結果

問題 No.577 Prime Powerful Numbers
ユーザー Sebastian KingSebastian King
提出日時 2023-12-02 22:37:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 4,850 bytes
コンパイル時間 2,053 ms
コンパイル使用メモリ 202,768 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-26 21:42:51
合計ジャッジ時間 2,783 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 5 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 15 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 75 ms
5,376 KB
testcase_06 AC 5 ms
5,376 KB
testcase_07 AC 76 ms
5,376 KB
testcase_08 AC 13 ms
5,376 KB
testcase_09 AC 13 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}

int sgn_pw_sub(ll p, ll q, ll target) {
  ll ret = 1;
  for (int i = 1; i <= q; ++i) {
    if (ret > target / p) return 1;
    ret *= p;
  } 
  if (ret > target) return 1;
  else if (ret == target) return 0;
  return -1;
}

bool check(ll x) {
  if (prime::is_prime(x)) return true;
  for (int b = 2; b <= 64; ++b) {
    ll q = exp(log(x + 0.5) / b);
    if (q == 0) return false;
    int sgn;
    while ((sgn = sgn_pw_sub(q + 1, b, x)) != 1) {
      q++;
    }
    if (prime::is_prime(q) && sgn_pw_sub(q, b, x) == 0) 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;
}
0