結果
| 問題 | No.308 素数は通れません |
| コンテスト | |
| ユーザー |
btk
|
| 提出日時 | 2015-12-02 00:12:53 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,274 bytes |
| 記録 | |
| コンパイル時間 | 1,493 ms |
| コンパイル使用メモリ | 165,524 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-09-14 07:23:25 |
| 合計ジャッジ時間 | 3,976 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 99 WA * 8 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
vector<bool> prime(10000,true);
bool solve(int w,int n){
vector<bool> used(30000,false);
used[1]=true;
used[0]=true;
queue<int> que;
que.push(1);
while(que.empty()==false){
int v=que.front();que.pop();
if(v%w!=1&&used[v-1]==false&&prime[v-1]==false){
que.push(v-1);
used[v-1]=true;
}
if(v%w!=0&&used[v+1]==false&&v+1<=n&&prime[v+1]==false){
que.push(v+1);
used[v+1]=true;
}
if(v+w<=n&&used[v+w]==false&&prime[v+w]==false){
que.push(v+w);
used[v+w]=true;
}
if(v-w>=1&&used[v-w]==false&&prime[v-w]==false){
que.push(v-w);
used[v-w]=true;
}
}
return used[n];
}
int main(){
string S;
cin>>S;
int a=7;
if(S.size()<4){
a=stoi(S);
for(int i=2;i<10000;i++){
if(prime[i]){
for(int j=i*2;j<10000;j+=i){
prime[j]=false;
}
}
}
for(int w = 2; w < a; w++){
if(solve(w,a)){
cout<<w<<endl;
return 0;
}
}
}
else cout<<8<<endl;
return 0;
}
btk