結果

問題 No.308 素数は通れません
ユーザー uafr_csuafr_cs
提出日時 2015-12-03 20:14:06
言語 Java21
(openjdk 21)
結果
AC  
実行時間 153 ms / 1,000 ms
コード長 3,430 bytes
コンパイル時間 2,504 ms
コンパイル使用メモリ 85,756 KB
実行使用メモリ 57,800 KB
最終ジャッジ日時 2023-08-18 13:20:54
合計ジャッジ時間 17,049 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
55,676 KB
testcase_01 AC 104 ms
56,076 KB
testcase_02 AC 106 ms
56,048 KB
testcase_03 AC 107 ms
55,908 KB
testcase_04 AC 108 ms
55,676 KB
testcase_05 AC 107 ms
55,684 KB
testcase_06 AC 106 ms
56,624 KB
testcase_07 AC 108 ms
55,640 KB
testcase_08 AC 107 ms
55,856 KB
testcase_09 AC 107 ms
55,888 KB
testcase_10 AC 107 ms
55,900 KB
testcase_11 AC 103 ms
56,004 KB
testcase_12 AC 108 ms
54,000 KB
testcase_13 AC 115 ms
55,720 KB
testcase_14 AC 113 ms
55,916 KB
testcase_15 AC 102 ms
55,752 KB
testcase_16 AC 107 ms
56,132 KB
testcase_17 AC 104 ms
55,980 KB
testcase_18 AC 105 ms
55,716 KB
testcase_19 AC 102 ms
56,104 KB
testcase_20 AC 108 ms
55,920 KB
testcase_21 AC 105 ms
55,996 KB
testcase_22 AC 107 ms
55,792 KB
testcase_23 AC 106 ms
56,008 KB
testcase_24 AC 109 ms
55,892 KB
testcase_25 AC 106 ms
55,772 KB
testcase_26 AC 102 ms
55,784 KB
testcase_27 AC 104 ms
55,640 KB
testcase_28 AC 107 ms
55,860 KB
testcase_29 AC 102 ms
55,548 KB
testcase_30 AC 106 ms
55,876 KB
testcase_31 AC 104 ms
56,048 KB
testcase_32 AC 109 ms
55,892 KB
testcase_33 AC 104 ms
55,992 KB
testcase_34 AC 103 ms
56,032 KB
testcase_35 AC 109 ms
55,892 KB
testcase_36 AC 106 ms
55,964 KB
testcase_37 AC 104 ms
55,696 KB
testcase_38 AC 101 ms
55,848 KB
testcase_39 AC 104 ms
55,752 KB
testcase_40 AC 105 ms
55,880 KB
testcase_41 AC 104 ms
56,036 KB
testcase_42 AC 103 ms
55,896 KB
testcase_43 AC 103 ms
55,692 KB
testcase_44 AC 104 ms
55,768 KB
testcase_45 AC 105 ms
55,828 KB
testcase_46 AC 104 ms
55,728 KB
testcase_47 AC 103 ms
55,688 KB
testcase_48 AC 103 ms
56,268 KB
testcase_49 AC 105 ms
56,012 KB
testcase_50 AC 104 ms
56,220 KB
testcase_51 AC 108 ms
55,944 KB
testcase_52 AC 106 ms
56,116 KB
testcase_53 AC 100 ms
55,668 KB
testcase_54 AC 104 ms
56,288 KB
testcase_55 AC 101 ms
56,008 KB
testcase_56 AC 102 ms
55,604 KB
testcase_57 AC 101 ms
55,776 KB
testcase_58 AC 103 ms
55,776 KB
testcase_59 AC 108 ms
55,540 KB
testcase_60 AC 106 ms
55,996 KB
testcase_61 AC 114 ms
55,964 KB
testcase_62 AC 109 ms
55,928 KB
testcase_63 AC 103 ms
55,968 KB
testcase_64 AC 108 ms
56,548 KB
testcase_65 AC 107 ms
55,776 KB
testcase_66 AC 103 ms
55,800 KB
testcase_67 AC 110 ms
55,608 KB
testcase_68 AC 103 ms
55,940 KB
testcase_69 AC 106 ms
55,816 KB
testcase_70 AC 103 ms
56,112 KB
testcase_71 AC 105 ms
56,088 KB
testcase_72 AC 106 ms
55,904 KB
testcase_73 AC 107 ms
57,716 KB
testcase_74 AC 102 ms
55,620 KB
testcase_75 AC 106 ms
55,996 KB
testcase_76 AC 114 ms
56,108 KB
testcase_77 AC 100 ms
55,352 KB
testcase_78 AC 108 ms
55,956 KB
testcase_79 AC 115 ms
55,840 KB
testcase_80 AC 132 ms
55,904 KB
testcase_81 AC 126 ms
56,008 KB
testcase_82 AC 111 ms
55,748 KB
testcase_83 AC 107 ms
56,060 KB
testcase_84 AC 144 ms
56,112 KB
testcase_85 AC 111 ms
55,992 KB
testcase_86 AC 128 ms
55,944 KB
testcase_87 AC 147 ms
56,060 KB
testcase_88 AC 104 ms
55,916 KB
testcase_89 AC 105 ms
55,536 KB
testcase_90 AC 121 ms
56,252 KB
testcase_91 AC 105 ms
57,800 KB
testcase_92 AC 105 ms
55,808 KB
testcase_93 AC 101 ms
55,964 KB
testcase_94 AC 106 ms
56,204 KB
testcase_95 AC 107 ms
56,012 KB
testcase_96 AC 106 ms
55,948 KB
testcase_97 AC 108 ms
55,948 KB
testcase_98 AC 106 ms
55,632 KB
testcase_99 AC 108 ms
55,628 KB
testcase_100 AC 104 ms
55,776 KB
testcase_101 AC 153 ms
57,052 KB
testcase_102 AC 106 ms
55,476 KB
testcase_103 AC 107 ms
55,788 KB
testcase_104 AC 105 ms
55,840 KB
testcase_105 AC 101 ms
55,644 KB
testcase_106 AC 102 ms
55,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class Main {
	
	public static int simulate(final int N){
		boolean[] is_prime = new boolean[N + 1];
		Arrays.fill(is_prime, 2, N + 1, true);
		
		for(int i = 2; i * i <= N; i++){
			if(!is_prime[i]){ continue; }
			
			for(int j = i * 2; j <= N; j += i){
				is_prime[j] = false;
			}
		}
		
		if(is_prime[N]){ return -1; }

		boolean[] visited = new boolean[N];
		for(int mid = 1; mid <= N; mid++){
			Arrays.fill(visited, false);

			
			LinkedList<Integer> queue = new LinkedList<Integer>();
			queue.add(0);
			visited[0] = true;
			while(!queue.isEmpty()){
				final int pos = queue.poll();
				final int number = pos + 1;
				
				if(number == N){ break; }
				//System.out.println(mid + " => " + pos + " : " + (pos % mid));
				
				if(pos % mid > 0 && !is_prime[number - 1] && !visited[pos - 1]){
					queue.add(pos - 1);
					visited[pos - 1] = true;
				}
				if(pos % mid < (mid - 1) && !is_prime[number + 1] && !visited[pos + 1]){
					queue.add(pos + 1);
					visited[pos + 1] = true;
				}
				if((pos - mid) > 0 && !is_prime[number - mid] && !visited[pos - mid]){
					queue.add(pos - mid);
					visited[pos - mid] = true;
				}
				if((pos + mid) < N && !is_prime[number + mid] && !visited[pos + mid]){
					queue.add(pos + mid);
					visited[pos + mid] = true;
				}
			}
			
			if(visited[N - 1]){
				return mid;
			}
		}
		
		return -1;
	}
	
	public static int smart(final BigInteger N){
		final BigInteger four = BigInteger.valueOf(4);
		final BigInteger six = BigInteger.valueOf(6);
		final BigInteger eight = BigInteger.valueOf(8);
		final BigInteger nine = BigInteger.valueOf(9);
		final BigInteger ten = BigInteger.valueOf(10);
		final BigInteger twelve = BigInteger.valueOf(12);
		final BigInteger fourteen = BigInteger.valueOf(14);
		final BigInteger fifteen = BigInteger.valueOf(15);
		final BigInteger sixteen = BigInteger.valueOf(16);
		final BigInteger twenty = BigInteger.valueOf(20);
		final BigInteger twentyone = BigInteger.valueOf(21);
		final BigInteger twentytwo = BigInteger.valueOf(22);
		final BigInteger twentyfour = BigInteger.valueOf(24);
		final BigInteger twentyfive = BigInteger.valueOf(25);
		
		if(N.equals(four)){
			return 3;
		}else if(N.equals(six)){
			return 5;
		}else if(N.equals(eight) || N.equals(nine) || N.equals(ten) || N.equals(fifteen) || N.equals(sixteen) || N.equals(twentytwo)){
			return 7;
		}else if(N.equals(twelve)){
			return 11;
		}else if(N.equals(fourteen)){
			return 13;
		}else if(N.equals(twenty) || N.equals(twentyone)){
			return 19;
		}else if(N.equals(twentyfour) || N.equals(twentyfive)){
			return 23;
		}else if(N.subtract(BigInteger.ONE).mod(eight).equals(BigInteger.ZERO) && N.subtract(eight).isProbablePrime(1000000000)){
			return 14;
		}else{
			return 8;
		}
	}
	
	// シュミレーションして法則性を掴むべき. 
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		/*
		for(int i = 2; i <= 10000; i++){
			final int sim_value = simulate(i);
			if(sim_value > 0){
				final int big_value = smart(BigInteger.valueOf(i));
				if(sim_value != big_value){
					System.out.printf("%05d : %d, %d\n", i, sim_value, big_value);
				}
			}
		}
		*/
		
		BigInteger N = new BigInteger(sc.next());
		
		System.out.println(smart(N));
		
	}

}
0