結果

問題 No.308 素数は通れません
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-18 16:02:38
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 308 ms / 1,000 ms
コード長 3,899 bytes
コンパイル時間 1,463 ms
コンパイル使用メモリ 160,728 KB
実行使用メモリ 8,400 KB
最終ジャッジ日時 2023-09-03 22:19:31
合計ジャッジ時間 10,331 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
4,376 KB
testcase_01 AC 8 ms
4,384 KB
testcase_02 AC 8 ms
4,380 KB
testcase_03 AC 8 ms
4,376 KB
testcase_04 AC 8 ms
4,376 KB
testcase_05 AC 9 ms
4,380 KB
testcase_06 AC 8 ms
4,376 KB
testcase_07 AC 8 ms
4,376 KB
testcase_08 AC 8 ms
4,380 KB
testcase_09 AC 8 ms
4,376 KB
testcase_10 AC 8 ms
4,376 KB
testcase_11 AC 8 ms
4,380 KB
testcase_12 AC 9 ms
4,376 KB
testcase_13 AC 8 ms
4,376 KB
testcase_14 AC 8 ms
4,380 KB
testcase_15 AC 8 ms
4,376 KB
testcase_16 AC 8 ms
4,376 KB
testcase_17 AC 8 ms
4,380 KB
testcase_18 AC 8 ms
4,376 KB
testcase_19 AC 9 ms
4,380 KB
testcase_20 AC 9 ms
4,376 KB
testcase_21 AC 8 ms
4,376 KB
testcase_22 AC 8 ms
4,376 KB
testcase_23 AC 8 ms
4,380 KB
testcase_24 AC 8 ms
4,376 KB
testcase_25 AC 8 ms
4,424 KB
testcase_26 AC 8 ms
4,380 KB
testcase_27 AC 9 ms
4,380 KB
testcase_28 AC 8 ms
4,376 KB
testcase_29 AC 8 ms
4,376 KB
testcase_30 AC 8 ms
4,376 KB
testcase_31 AC 8 ms
4,376 KB
testcase_32 AC 8 ms
4,376 KB
testcase_33 AC 9 ms
4,376 KB
testcase_34 AC 8 ms
4,376 KB
testcase_35 AC 8 ms
4,376 KB
testcase_36 AC 42 ms
7,720 KB
testcase_37 AC 43 ms
8,052 KB
testcase_38 AC 42 ms
7,768 KB
testcase_39 AC 40 ms
7,464 KB
testcase_40 AC 43 ms
7,712 KB
testcase_41 AC 46 ms
8,044 KB
testcase_42 AC 38 ms
7,544 KB
testcase_43 AC 42 ms
7,772 KB
testcase_44 AC 39 ms
7,516 KB
testcase_45 AC 50 ms
8,004 KB
testcase_46 AC 46 ms
8,044 KB
testcase_47 AC 51 ms
8,012 KB
testcase_48 AC 33 ms
6,712 KB
testcase_49 AC 46 ms
8,040 KB
testcase_50 AC 43 ms
7,992 KB
testcase_51 AC 47 ms
8,036 KB
testcase_52 AC 28 ms
6,464 KB
testcase_53 AC 40 ms
7,452 KB
testcase_54 AC 55 ms
7,996 KB
testcase_55 AC 51 ms
8,012 KB
testcase_56 AC 32 ms
7,188 KB
testcase_57 AC 44 ms
8,000 KB
testcase_58 AC 37 ms
7,188 KB
testcase_59 AC 57 ms
7,980 KB
testcase_60 AC 63 ms
8,000 KB
testcase_61 AC 50 ms
8,024 KB
testcase_62 AC 40 ms
7,524 KB
testcase_63 AC 82 ms
8,400 KB
testcase_64 AC 86 ms
7,988 KB
testcase_65 AC 136 ms
8,020 KB
testcase_66 AC 51 ms
8,048 KB
testcase_67 AC 61 ms
7,972 KB
testcase_68 AC 98 ms
8,372 KB
testcase_69 AC 23 ms
5,436 KB
testcase_70 AC 94 ms
8,352 KB
testcase_71 AC 48 ms
7,716 KB
testcase_72 AC 63 ms
8,032 KB
testcase_73 AC 61 ms
8,296 KB
testcase_74 AC 161 ms
8,004 KB
testcase_75 AC 26 ms
6,132 KB
testcase_76 AC 128 ms
8,256 KB
testcase_77 AC 42 ms
7,748 KB
testcase_78 AC 58 ms
8,000 KB
testcase_79 AC 23 ms
6,456 KB
testcase_80 AC 159 ms
8,272 KB
testcase_81 AC 237 ms
8,304 KB
testcase_82 AC 51 ms
8,040 KB
testcase_83 AC 97 ms
8,036 KB
testcase_84 AC 139 ms
8,304 KB
testcase_85 AC 47 ms
7,976 KB
testcase_86 AC 148 ms
8,272 KB
testcase_87 AC 304 ms
8,280 KB
testcase_88 AC 83 ms
8,004 KB
testcase_89 AC 79 ms
7,992 KB
testcase_90 AC 214 ms
8,308 KB
testcase_91 AC 114 ms
8,048 KB
testcase_92 AC 72 ms
8,044 KB
testcase_93 AC 113 ms
8,048 KB
testcase_94 AC 75 ms
8,004 KB
testcase_95 AC 80 ms
8,040 KB
testcase_96 AC 17 ms
4,880 KB
testcase_97 AC 25 ms
6,196 KB
testcase_98 AC 25 ms
5,404 KB
testcase_99 AC 52 ms
7,956 KB
testcase_100 AC 70 ms
7,980 KB
testcase_101 AC 308 ms
8,256 KB
testcase_102 AC 93 ms
8,000 KB
testcase_103 AC 25 ms
5,728 KB
testcase_104 AC 78 ms
8,032 KB
testcase_105 AC 121 ms
8,392 KB
testcase_106 AC 25 ms
5,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, 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; }

void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }

immutable LIM = 10^^6;
bool[] isnp;

void prepare() {
  isnp = new bool[LIM];
  isnp[0] = isnp[1] = true;
  foreach (i; 2 .. LIM) {
    if (!isnp[i]) {
      for (int j = 2 * i; j < LIM; j += i) {
        isnp[j] = true;
      }
    }
  }
}

bool[][] doIt(int H, int W, int N) {
  auto A = new bool[][](H, W);
  foreach (x; 0 .. H) foreach (y; 0 .. W) {
    A[x][y] = (1 + x * W + y <= N) && isnp[1 + x * W + y];
  }
  auto vis = new bool[][](H, W);
  void dfs(int x, int y) {
    vis[x][y] = true;
    foreach (dir; 0 .. 4) {
      const xx = x + [+1, 0, -1, 0][dir];
      const yy = y + [0, +1, 0, -1][dir];
      if (0 <= xx && xx < H && 0 <= yy && yy < W && A[xx][yy]) {
        if (!vis[xx][yy]) {
          dfs(xx, yy);
        }
      }
    }
  }
  dfs(0, 0);
  return vis;
}

int solveSmall(int N) {
  foreach (W; 1 .. N) {
    const H = (N + W + 1) / W;
    const vis = doIt(H, W, N);
    if (vis[(N - 1) / W][(N - 1) % W]) {
      return W;
    }
  }
  return -1;
}

bool isPrime(BigInt n) {
  if (n <= 1 || n % 2 == 0) return (n == 2);
  BigInt power(BigInt a, BigInt e, BigInt m) {
    BigInt x = a, y = 1;
    for (; e != 0; e >>= 1, x = x * x % m) if (e & 1) y = y * x % m;
    return y;
  }
  BigInt d;
  int r, s;
  for (d = n - 1, s = 0; d % 2 == 0; d >>= 1, ++s) {}
  foreach (a; 2 .. 100) if (a < n) {
    BigInt ad = power(BigInt(a), d, n);
    if (ad == 1) continue;
    for (r = 0; r < s; ++r) {
      if (ad == n - 1) break;
      ad = ad * ad % n;
    }
    if (r == s) return false;
  }
  return true;
}

void main() {
  prepare();
  
  /*
  foreach (W; 1 .. 30) {
    const vis = doIt(2 * W, W, 2 * W * W);
    foreach (x; 0 .. 2 * W) {
      foreach (y; 0 .. W) {
        writef("%s%4s%s", vis[x][y] ? "[" : " ", 1 + x * W + y, vis[x][y] ? "]" : " ");
      }
      writeln();
    }
    writeln();
  }
  return;
  */
  
  try {
    for (; ; ) {
      const N = readToken().to!BigInt;
      int ans;
      if (N <= 20 * 20) {
        ans = solveSmall(cast(int)(N));
      } else {
        foreach (W; [8, 14, 17, 19, 20]) {
          auto vis = new bool[W^^2];
          bool ok;
          void dfs(BigInt n) {
            if (n <= N && !isPrime(n)) {
              if (N - n >= W^^2) {
                ok = true;
                return;
              }
              if (!vis[cast(int)(N - n)]) {
                vis[cast(int)(N - n)] = true;
                const y = cast(int)((n - 1) % W);
                dfs(n - W);
                dfs(n + W);
                if (0 <= y - 1) {
                  dfs(n - 1);
                }
                if (y + 1 < W) {
                  dfs(n + 1);
                }
              }
            }
          }
          dfs(N);
          if (ok) {
            ans = W;
            break;
          }
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0