結果
| 問題 |
No.308 素数は通れません
|
| コンテスト | |
| ユーザー |
climpet
|
| 提出日時 | 2015-12-01 02:06:00 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 978 bytes |
| コンパイル時間 | 820 ms |
| コンパイル使用メモリ | 66,840 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-14 06:16:32 |
| 合計ジャッジ時間 | 3,039 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 99 WA * 8 |
ソースコード
#include <iostream>
#include <string>
#include <cstdlib>
#include <queue>
#include <cassert>
using namespace std;
int main(){
const int M = 200;
int dd[] = {0, -1, 0, 1, 0};
bool npr[M + 1] = {true, true};
for(int i = 2; i <= M; ++i){
if(!npr[i]){
for(int j = i * 2; j <= M; j += i){
npr[j] = true;
}
}
}
string s;
cin >> s;
int n;
if(s.size() > 2){
n = 50;
}
else{
n = strtol(s.c_str(), 0, 10);
}
if(n > 50){ n = 50; }
int ans;
for(ans = 3; ; ++ans){
assert(ans < n);
vector<char> vis(n + 1);
vis[1] = 1;
queue<int> q;
q.push(1);
while(!q.empty()){
int y = (q.front() - 1) / ans;
int x = (q.front() - 1) % ans;
q.pop();
for(int i = 0; i < 4; ++i){
int ny = y + dd[i], nx = x + dd[i + 1];
if(ny < 0 || nx < 0 || nx >= ans){ continue; }
int t = ny * ans + nx + 1;
if(t > n || !npr[t] || vis[t]){ continue; }
vis[t] = 1;
q.push(t);
}
}
if(vis[n]){ break; }
}
cout << ans << endl;
}
climpet