結果
| 問題 | No.308 素数は通れません |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-01-18 15:02:48 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,460 bytes |
| 記録 | |
| コンパイル時間 | 917 ms |
| コンパイル使用メモリ | 118,084 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-06-13 02:57:50 |
| 合計ジャッジ時間 | 3,882 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 96 WA * 11 |
ソースコード
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)); }
/*
01
02
01 02
03
01 02 03
04 05 06
07
01 02 03 04
05
01 02 03 04 05
06 07
11
01 02 03 04 05 06
07
01 02 03 04 05 06 07
08 09 10 11
15 16 17
22 23
29
01 02 03 04 05 06 07 08
09 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29
*/
immutable LIM = 100;
bool[] isnp;
void prepare() {
isnp = new bool[LIM];
isnp[0] = isnp[1] = true;
foreach (i; 2 .. LIM) {
if (!isnp[i]) {
for (int j = i * i; j < LIM; j += i) {
isnp[j] = true;
}
}
}
}
int solveSmall(int N) {
foreach (W; 1 .. N) {
const H = (N + W + 1) / W;
auto A = new bool[][](H, W);
foreach (x; 0 .. H) foreach (y; 0 .. W) {
A[x][y] = 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[x][y]) {
if (!vis[xx][yy]) {
dfs(xx, yy);
}
}
}
}
dfs(0, 0);
if (vis[(N - 1) / W][(N - 1) % W]) {
return W;
}
}
return -1;
}
void main() {
prepare();
try {
for (; ; ) {
const N = readToken();
int ans;
if (N.length <= 2) {
ans = solveSmall(N.to!int);
} else {
ans = 8;
}
writeln(ans);
}
} catch (EOFException e) {
}
}