結果

問題 No.577 Prime Powerful Numbers
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-04-20 17:12:20
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 1,336 ms / 2,000 ms
コード長 3,882 bytes
コンパイル時間 1,660 ms
コンパイル使用メモリ 153,516 KB
実行使用メモリ 35,456 KB
最終ジャッジ日時 2023-09-04 00:44:00
合計ジャッジ時間 7,976 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
21,540 KB
testcase_01 AC 105 ms
25,028 KB
testcase_02 AC 54 ms
21,852 KB
testcase_03 AC 673 ms
34,388 KB
testcase_04 AC 100 ms
24,112 KB
testcase_05 AC 1,070 ms
33,680 KB
testcase_06 AC 1,336 ms
34,936 KB
testcase_07 AC 1,103 ms
35,456 KB
testcase_08 AC 599 ms
34,392 KB
testcase_09 AC 406 ms
34,172 KB
testcase_10 AC 51 ms
20,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.range, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


// a b (mod m)
ulong multiply(ulong a, ulong b, ulong m)
in {
  assert(m < 1UL << 63, "multiply: m < 2^63 must hold");
  assert(a < m, "multiply: a < m must hold");
  assert(b < m, "multiply: b < m must hold");
}
do {
  ulong c = 0;
  for (; a; a >>= 1) {
    if (a & 1) {
      c += b;
      if (c >= m) c -= m;
    }
    b <<= 1;
    if (b >= m) b -= m;
  }
  return c;
}

// a^e (mod m)
ulong power(ulong a, ulong e, ulong m)
in {
  assert(m < 1UL << 63, "power: m < 2^63 must hold");
  assert(a < m, "power: a < m must hold");
}
do {
  long b = 1;
  for (; e; e >>= 1) {
    if (e & 1) b = multiply(b, a, m);
    a = multiply(a, a, m);
  }
  return b;
}

// Checks if n is a prime using Miller-Rabin test
bool isPrime(ulong n)
in {
  assert(n < 1UL << 63, "isPrime: n < 2^63 must hold");
}
do {
  import core.bitop : bsf;
  // http://miller-rabin.appspot.com/
  enum ulong[] BASES = [2, 325, 9375, 28178, 450775, 9780504, 1795265022];
  if (n <= 1 || !(n & 1)) return (n == 2);
  const s = bsf(n - 1);
  const d = (n - 1) >> s;
  foreach (base; BASES) {
    ulong a = base % n;
    if (a == 0) continue;
    a = power(a, d, n);
    if (a == 1 || a == n - 1) continue;
    bool ok = false;
    foreach (_; 0 .. s - 1) {
      a = multiply(a, a, n);
      if (a == n - 1) {
        ok = true;
        break;
      }
    }
    if (!ok) return false;
  }
  return true;
}


enum LIM = 10L^^18;
enum SMALL = 10^^6;

void main() {
  auto isnp = new bool[SMALL];
  foreach (p; 2 .. SMALL) {
    if (!isnp[p]) {
      for (int n = 2 * p; n < SMALL; n += p) {
        isnp[n] = true;
      }
    }
  }
  bool[long] isPrimePower;
  foreach (p; 2 .. SMALL) {
    if (!isnp[p]) {
      long pk = 1;
      for (; pk <= LIM / p; ) {
        pk *= p;
        isPrimePower[pk] = true;
      }
    }
  }
  
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        stderr.writefln("==== Case #%d ====", caseId);
        stderr.flush;
        const N = readLong();
        bool ans;
        if (N % 2 == 0) {
          ans = (N >= 4);
        } else {
          for (long a = 1; (1L << a) < N; ++a) {
            const m = N - (1L << a);
            if (m in isPrimePower) {
              ans = true;
            }
            if (isPrime(m)) {
              ans = true;
            }
            long lo = 0, hi = m;
            for (; lo + 1 < hi; ) {
              const mid = (lo + hi) / 2;
              (BigInt(mid) * mid >= m) ? (hi = mid) : (lo = mid);
            }
            if (hi * hi == m) {
              if (isPrime(hi)) {
                ans = true;
              }
            }
          }
        }
        writeln(ans ? "Yes" : "No");
      }
    }
  } catch (EOFException e) {
  }
}
0