結果
| 問題 |
No.308 素数は通れません
|
| コンテスト | |
| ユーザー |
kuuso1
|
| 提出日時 | 2015-12-04 01:26:00 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 41 ms / 1,000 ms |
| コード長 | 2,928 bytes |
| コンパイル時間 | 1,413 ms |
| コンパイル使用メモリ | 113,896 KB |
| 実行使用メモリ | 24,500 KB |
| 最終ジャッジ日時 | 2024-11-27 18:38:32 |
| 合計ジャッジ時間 | 7,868 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 107 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
using BI=System.Numerics.BigInteger;
class TEST{
static void Main(){
Sol mySol =new Sol();
mySol.Solve();
}
}
class Sol{
public void Solve(){
int ans=0;
int N2=0;
if(N<100){
N2=(int)N;
switch(N2){
case 4: ans=3;break;
case 6: ans=5;break;
case 8: ans=7;break;
case 9: ans=7;break;
case 10: ans=7;break;
case 15: ans=7;break;
case 16: ans=7;break;
case 22: ans=7;break;
case 33: ans=8;break;
case 57: ans=8;break;
case 65: ans=8;break;
case 12: ans=11;break;
case 14: ans=13;break;
case 20: ans=19;break;
case 21: ans=19;break;
case 24: ans=23;break;
case 25: ans=23;break;
default:
if(N%8==1){
ans=14;
}else{
ans=8;
}break;
}
Console.WriteLine(ans);
return;
}
Console.WriteLine((N%8==1 && IsPrime(N-8))?14:8);
}
String S;
BI N;
public Sol(){
S=rs();
N=BI.Parse(S);
}
static bool IsPrime(BI x){
if(x==2)return true;
if((x==1) || (x&1)==0)return false;
if(x<(BI)1e12){
for(BI t=2;t*t<=x;t++){
if(x%t==0)return false;
}
return true;
}
return MillerRabinTest(x);
}
static bool MillerRabinTest(BI n){
int rep=20;
BI d=n-1;
// (n-1)=2^s*d
while((d&1)==0)d>>=1;
var rnd=new Xor128();
BI a=1+((BI)rnd.Next())%(n-2);
while(rep>0){
rep--;
a=1+((BI)rnd.Next()*a)%(n-2);
BI t=d;
BI y=ModPowBI(a,t,n);
while(t!=(n-1) && y!=1 && y!=n-1){
y=y*y%n;
t<<=1;
}
if(y!=(n-1) && (t&1)==0)return false;
}
return true;
}
static BI ModPowBI(BI a,BI t,BI mod){
BI ret=1;
while(t>0){
if((t&1)==1){ret*=a;ret%=mod;};
a=a*a;a%=mod;
t>>=1;
}
return ret;
}
static String rs(){return Console.ReadLine();}
static int ri(){return int.Parse(Console.ReadLine());}
static long rl(){return long.Parse(Console.ReadLine());}
static double rd(){return double.Parse(Console.ReadLine());}
static String[] rsa(){return Console.ReadLine().Split(' ');}
static int[] ria(){return Array.ConvertAll(Console.ReadLine().Split(' '),e=>int.Parse(e));}
static long[] rla(){return Array.ConvertAll(Console.ReadLine().Split(' '),e=>long.Parse(e));}
static double[] rda(){return Array.ConvertAll(Console.ReadLine().Split(' '),e=>double.Parse(e));}
}
class Xor128{
static uint x = 123456789;
static uint y = 362436069;
static uint z = 521288629;
static uint w = 88675123;
uint t;
public Xor128(){
}
public Xor128(uint seed){
z ^= seed;
z ^= z >> 21; z ^= z << 35; z ^= z >> 4;
}
public uint Next(){
t = x ^ (x << 11);
x = y; y = z; z = w;
return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}
public int Next(int ul){
return NextI(0,ul-1);
}
public int NextI(int from,int to){
int mod=to-from+1;
int ret=(int)(Next()%mod);
return ret+from;
}
}
kuuso1