結果
問題 | No.308 素数は通れません |
ユーザー | 👑 hos.lyric |
提出日時 | 2019-01-18 16:02:38 |
言語 | D (dmd 2.106.1) |
結果 |
AC
|
実行時間 | 257 ms / 1,000 ms |
コード長 | 3,899 bytes |
コンパイル時間 | 1,717 ms |
コンパイル使用メモリ | 164,172 KB |
実行使用メモリ | 7,916 KB |
最終ジャッジ日時 | 2024-06-13 02:58:03 |
合計ジャッジ時間 | 8,588 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 7 ms
6,816 KB |
testcase_01 | AC | 7 ms
6,944 KB |
testcase_02 | AC | 8 ms
6,940 KB |
testcase_03 | AC | 7 ms
6,944 KB |
testcase_04 | AC | 7 ms
6,940 KB |
testcase_05 | AC | 7 ms
6,940 KB |
testcase_06 | AC | 7 ms
6,940 KB |
testcase_07 | AC | 8 ms
6,944 KB |
testcase_08 | AC | 7 ms
6,940 KB |
testcase_09 | AC | 7 ms
6,944 KB |
testcase_10 | AC | 8 ms
6,940 KB |
testcase_11 | AC | 7 ms
6,944 KB |
testcase_12 | AC | 7 ms
6,940 KB |
testcase_13 | AC | 8 ms
6,940 KB |
testcase_14 | AC | 9 ms
6,940 KB |
testcase_15 | AC | 7 ms
6,944 KB |
testcase_16 | AC | 8 ms
6,944 KB |
testcase_17 | AC | 7 ms
6,940 KB |
testcase_18 | AC | 8 ms
6,940 KB |
testcase_19 | AC | 8 ms
6,944 KB |
testcase_20 | AC | 9 ms
6,944 KB |
testcase_21 | AC | 8 ms
6,940 KB |
testcase_22 | AC | 7 ms
6,940 KB |
testcase_23 | AC | 8 ms
6,944 KB |
testcase_24 | AC | 8 ms
6,940 KB |
testcase_25 | AC | 8 ms
6,944 KB |
testcase_26 | AC | 8 ms
6,940 KB |
testcase_27 | AC | 8 ms
6,940 KB |
testcase_28 | AC | 7 ms
6,940 KB |
testcase_29 | AC | 8 ms
6,940 KB |
testcase_30 | AC | 8 ms
6,944 KB |
testcase_31 | AC | 8 ms
6,940 KB |
testcase_32 | AC | 7 ms
6,940 KB |
testcase_33 | AC | 8 ms
6,944 KB |
testcase_34 | AC | 8 ms
6,940 KB |
testcase_35 | AC | 8 ms
6,940 KB |
testcase_36 | AC | 38 ms
7,372 KB |
testcase_37 | AC | 39 ms
7,484 KB |
testcase_38 | AC | 39 ms
7,416 KB |
testcase_39 | AC | 34 ms
7,188 KB |
testcase_40 | AC | 38 ms
7,460 KB |
testcase_41 | AC | 40 ms
7,704 KB |
testcase_42 | AC | 34 ms
7,032 KB |
testcase_43 | AC | 38 ms
7,340 KB |
testcase_44 | AC | 34 ms
7,028 KB |
testcase_45 | AC | 45 ms
7,756 KB |
testcase_46 | AC | 43 ms
7,736 KB |
testcase_47 | AC | 47 ms
7,692 KB |
testcase_48 | AC | 30 ms
6,940 KB |
testcase_49 | AC | 40 ms
7,672 KB |
testcase_50 | AC | 38 ms
7,576 KB |
testcase_51 | AC | 42 ms
7,832 KB |
testcase_52 | AC | 26 ms
6,944 KB |
testcase_53 | AC | 36 ms
7,076 KB |
testcase_54 | AC | 47 ms
7,740 KB |
testcase_55 | AC | 44 ms
7,688 KB |
testcase_56 | AC | 28 ms
6,944 KB |
testcase_57 | AC | 39 ms
7,576 KB |
testcase_58 | AC | 33 ms
6,944 KB |
testcase_59 | AC | 50 ms
7,736 KB |
testcase_60 | AC | 54 ms
7,760 KB |
testcase_61 | AC | 43 ms
7,696 KB |
testcase_62 | AC | 34 ms
6,940 KB |
testcase_63 | AC | 69 ms
7,720 KB |
testcase_64 | AC | 74 ms
7,716 KB |
testcase_65 | AC | 114 ms
7,724 KB |
testcase_66 | AC | 43 ms
7,752 KB |
testcase_67 | AC | 54 ms
7,716 KB |
testcase_68 | AC | 83 ms
7,760 KB |
testcase_69 | AC | 20 ms
6,944 KB |
testcase_70 | AC | 79 ms
7,804 KB |
testcase_71 | AC | 41 ms
7,508 KB |
testcase_72 | AC | 53 ms
7,680 KB |
testcase_73 | AC | 52 ms
7,744 KB |
testcase_74 | AC | 137 ms
7,804 KB |
testcase_75 | AC | 23 ms
6,940 KB |
testcase_76 | AC | 110 ms
7,752 KB |
testcase_77 | AC | 38 ms
7,292 KB |
testcase_78 | AC | 51 ms
7,788 KB |
testcase_79 | AC | 22 ms
6,944 KB |
testcase_80 | AC | 140 ms
7,752 KB |
testcase_81 | AC | 192 ms
7,860 KB |
testcase_82 | AC | 42 ms
7,764 KB |
testcase_83 | AC | 79 ms
7,856 KB |
testcase_84 | AC | 115 ms
7,784 KB |
testcase_85 | AC | 41 ms
7,596 KB |
testcase_86 | AC | 124 ms
7,860 KB |
testcase_87 | AC | 249 ms
7,872 KB |
testcase_88 | AC | 71 ms
7,720 KB |
testcase_89 | AC | 67 ms
7,744 KB |
testcase_90 | AC | 184 ms
7,800 KB |
testcase_91 | AC | 97 ms
7,756 KB |
testcase_92 | AC | 61 ms
7,688 KB |
testcase_93 | AC | 98 ms
7,684 KB |
testcase_94 | AC | 64 ms
7,684 KB |
testcase_95 | AC | 70 ms
7,840 KB |
testcase_96 | AC | 15 ms
6,944 KB |
testcase_97 | AC | 21 ms
6,940 KB |
testcase_98 | AC | 22 ms
6,948 KB |
testcase_99 | AC | 44 ms
7,724 KB |
testcase_100 | AC | 58 ms
7,700 KB |
testcase_101 | AC | 257 ms
7,916 KB |
testcase_102 | AC | 76 ms
7,748 KB |
testcase_103 | AC | 21 ms
6,944 KB |
testcase_104 | AC | 66 ms
7,684 KB |
testcase_105 | AC | 101 ms
7,692 KB |
testcase_106 | AC | 23 ms
6,948 KB |
ソースコード
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) { } }