結果
| 問題 | No.308 素数は通れません |
| コンテスト | |
| ユーザー |
btk
|
| 提出日時 | 2015-12-08 20:38:03 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,285 bytes |
| 記録 | |
| コンパイル時間 | 1,418 ms |
| コンパイル使用メモリ | 167,132 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-14 20:23:20 |
| 合計ジャッジ時間 | 4,128 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 96 WA * 11 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef __int128 Int;
vector<bool> prime(100,true);
void eratosthenes(){
for(int i=2;i<100;i++){
if(prime[i]){
for(int j=i*2;j<100;j+=i){
prime[j]=false;
}
}
}
}
bool bfs(int w,int n){
vector<bool> used(1000,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 mulmod(Int a,Int n,Int Mod){
Int res=0;
while(n){
if(n&1)res=(res+a)%Mod;
a=(a+a)%Mod;
n>>=1;
}
return res;
}
Int powmod(Int a,Int n,Int Mod){
Int res=1;
while(n){
if(n&1)res=mulmod(res,a,Mod);
a=mulmod(a,a,Mod);
n>>=1;
}
return res;
}
bool miller_rabin(Int n){
Int d=n-1,s=0;
while(d%2==0){
d/=2;
s+=1;
}
for(int k=2;k<100;k++){
if(!prime[k])continue;
Int a=k;
if(powmod(a,d,n)!=1){
bool b=true;
for(Int r=0,_d=d;r<s;r++,_d<<=1){
b&=(powmod(a,_d,n)!=n-1);
//print(powmod(a,_d,n));
if(powmod(a,_d,n)<0)cout<<"ERROR"<<endl;
}
if(b){return false;}
}
}
return true;
}
int main(){
eratosthenes();
string S;
cin>>S;
if(S.size()<2){
int a=stoi(S);
for(int w = 2; w < a; w++)
if(bfs(w,a)){
cout<<w<<endl;
return 0;
}
}
else {
Int a=0;
for(auto& it : S){
a*=10;
a+=(it-'0');
}
a-=8;
if((a%8==1)&&miller_rabin(a))cout<<14<<endl;
else cout<<8<<endl;
}
return 0;
}
btk