結果
問題 |
No.308 素数は通れません
|
ユーザー |
|
提出日時 | 2015-12-01 00:23:35 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,587 bytes |
コンパイル時間 | 1,515 ms |
コンパイル使用メモリ | 170,656 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-14 05:29:04 |
合計ジャッジ時間 | 3,880 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 99 WA * 8 |
ソースコード
#include <bits/stdc++.h> #define GET_MACRO(a, b, c, NAME, ...) NAME #define rep(...) GET_MACRO(__VA_ARGS__, rep3, rep2)(__VA_ARGS__) #define rep2(i, a) rep3 (i, 0, a) #define rep3(i, a, b) for (int i = (a); i < (b); i++) #define repr(...) GET_MACRO(__VA_ARGS__, repr3, repr2)(__VA_ARGS__) #define repr2(i, a) repr3 (i, 0, a) #define repr3(i, a, b) for (int i = (b) - 1; i >= (a); i--) using namespace std; typedef long long ll; template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); } template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template<class T> T parse(string s) { istringstream iss(s); T res; iss >> res; return res; } bool isprime(ll n) { for (ll i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return n != 1; } bool check(int n, int w) { queue<pair<int, int>> q; vector<vector<char>> vis(n + 1, vector<char>(w)); q.emplace(0, 0); vis[0][0] = true; n--; while (!q.empty()) { int y, x; tie(y, x) = q.front(); q.pop(); int dy[] = {0, 1, 0, -1}; int dx[] = {1, 0, -1, 0}; rep (k, 4) { int ny = y + dy[k]; int nx = x + dx[k]; int nv = ny * w + nx; if (ny < 0 || nx < 0 || nx >= w || nv > n) continue; if (isprime(nv + 1)) continue; if (vis[ny][nx]) continue; vis[ny][nx] = true; q.emplace(ny, nx); } } return vis[n / w][n % w]; } int main() { string N; cin >> N; if (N.length() >= 3) { cout << 8 << endl; } else { int n = parse<int>(N); int ans = 1; while (!check(n, ans)) ans++; cout << ans << endl; } return 0; }