結果

問題 No.308 素数は通れません
ユーザー kuuso1kuuso1
提出日時 2015-12-04 01:26:00
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 61 ms / 1,000 ms
コード長 2,928 bytes
コンパイル時間 4,418 ms
コンパイル使用メモリ 107,324 KB
実行使用メモリ 22,944 KB
最終ジャッジ日時 2023-08-18 13:20:10
合計ジャッジ時間 10,529 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
20,740 KB
testcase_01 AC 50 ms
20,840 KB
testcase_02 AC 49 ms
20,736 KB
testcase_03 AC 49 ms
20,824 KB
testcase_04 AC 51 ms
20,840 KB
testcase_05 AC 50 ms
20,784 KB
testcase_06 AC 51 ms
20,840 KB
testcase_07 AC 50 ms
22,912 KB
testcase_08 AC 51 ms
20,816 KB
testcase_09 AC 52 ms
20,812 KB
testcase_10 AC 49 ms
20,828 KB
testcase_11 AC 49 ms
20,932 KB
testcase_12 AC 50 ms
22,788 KB
testcase_13 AC 51 ms
20,836 KB
testcase_14 AC 54 ms
20,820 KB
testcase_15 AC 53 ms
22,932 KB
testcase_16 AC 50 ms
22,876 KB
testcase_17 AC 49 ms
20,768 KB
testcase_18 AC 51 ms
20,720 KB
testcase_19 AC 50 ms
20,816 KB
testcase_20 AC 50 ms
22,864 KB
testcase_21 AC 50 ms
22,868 KB
testcase_22 AC 50 ms
20,860 KB
testcase_23 AC 50 ms
22,880 KB
testcase_24 AC 49 ms
18,820 KB
testcase_25 AC 50 ms
22,784 KB
testcase_26 AC 50 ms
22,756 KB
testcase_27 AC 52 ms
18,824 KB
testcase_28 AC 50 ms
22,756 KB
testcase_29 AC 53 ms
20,696 KB
testcase_30 AC 51 ms
22,792 KB
testcase_31 AC 50 ms
20,788 KB
testcase_32 AC 53 ms
22,800 KB
testcase_33 AC 51 ms
20,784 KB
testcase_34 AC 50 ms
20,844 KB
testcase_35 AC 51 ms
20,876 KB
testcase_36 AC 50 ms
20,864 KB
testcase_37 AC 53 ms
20,780 KB
testcase_38 AC 50 ms
20,840 KB
testcase_39 AC 50 ms
22,744 KB
testcase_40 AC 50 ms
22,912 KB
testcase_41 AC 52 ms
20,720 KB
testcase_42 AC 51 ms
22,876 KB
testcase_43 AC 50 ms
22,896 KB
testcase_44 AC 50 ms
22,864 KB
testcase_45 AC 58 ms
20,796 KB
testcase_46 AC 52 ms
20,740 KB
testcase_47 AC 50 ms
20,800 KB
testcase_48 AC 51 ms
18,792 KB
testcase_49 AC 51 ms
20,864 KB
testcase_50 AC 52 ms
20,680 KB
testcase_51 AC 51 ms
20,740 KB
testcase_52 AC 51 ms
20,792 KB
testcase_53 AC 50 ms
18,828 KB
testcase_54 AC 50 ms
20,836 KB
testcase_55 AC 50 ms
20,828 KB
testcase_56 AC 51 ms
20,788 KB
testcase_57 AC 51 ms
22,788 KB
testcase_58 AC 50 ms
22,932 KB
testcase_59 AC 49 ms
20,764 KB
testcase_60 AC 52 ms
20,836 KB
testcase_61 AC 52 ms
22,892 KB
testcase_62 AC 51 ms
18,800 KB
testcase_63 AC 57 ms
22,848 KB
testcase_64 AC 51 ms
20,800 KB
testcase_65 AC 50 ms
20,844 KB
testcase_66 AC 51 ms
20,908 KB
testcase_67 AC 51 ms
20,792 KB
testcase_68 AC 52 ms
20,720 KB
testcase_69 AC 53 ms
20,816 KB
testcase_70 AC 51 ms
20,852 KB
testcase_71 AC 49 ms
20,832 KB
testcase_72 AC 50 ms
22,796 KB
testcase_73 AC 49 ms
20,896 KB
testcase_74 AC 52 ms
20,688 KB
testcase_75 AC 52 ms
20,792 KB
testcase_76 AC 53 ms
18,764 KB
testcase_77 AC 49 ms
20,864 KB
testcase_78 AC 51 ms
20,688 KB
testcase_79 AC 49 ms
20,724 KB
testcase_80 AC 53 ms
22,820 KB
testcase_81 AC 58 ms
18,772 KB
testcase_82 AC 50 ms
22,888 KB
testcase_83 AC 51 ms
20,732 KB
testcase_84 AC 56 ms
20,868 KB
testcase_85 AC 50 ms
18,712 KB
testcase_86 AC 56 ms
22,892 KB
testcase_87 AC 57 ms
22,900 KB
testcase_88 AC 51 ms
18,704 KB
testcase_89 AC 54 ms
22,836 KB
testcase_90 AC 61 ms
20,832 KB
testcase_91 AC 51 ms
22,760 KB
testcase_92 AC 51 ms
20,752 KB
testcase_93 AC 49 ms
20,816 KB
testcase_94 AC 50 ms
22,768 KB
testcase_95 AC 50 ms
20,784 KB
testcase_96 AC 58 ms
20,816 KB
testcase_97 AC 51 ms
22,832 KB
testcase_98 AC 50 ms
22,884 KB
testcase_99 AC 55 ms
20,776 KB
testcase_100 AC 55 ms
22,852 KB
testcase_101 AC 59 ms
22,944 KB
testcase_102 AC 54 ms
20,860 KB
testcase_103 AC 53 ms
22,812 KB
testcase_104 AC 52 ms
22,824 KB
testcase_105 AC 53 ms
20,768 KB
testcase_106 AC 58 ms
18,864 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

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;
	}
}
0