結果

問題 No.12 限定された素数
ユーザー uafr_csuafr_cs
提出日時 2015-12-06 21:50:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 524 ms / 5,000 ms
コード長 2,086 bytes
コンパイル時間 2,623 ms
コンパイル使用メモリ 76,880 KB
実行使用メモリ 89,684 KB
最終ジャッジ日時 2023-08-15 23:10:07
合計ジャッジ時間 17,867 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 460 ms
89,076 KB
testcase_01 AC 424 ms
88,692 KB
testcase_02 AC 415 ms
87,572 KB
testcase_03 AC 499 ms
88,076 KB
testcase_04 AC 429 ms
88,988 KB
testcase_05 AC 430 ms
89,224 KB
testcase_06 AC 436 ms
88,988 KB
testcase_07 AC 439 ms
89,072 KB
testcase_08 AC 423 ms
89,108 KB
testcase_09 AC 436 ms
89,108 KB
testcase_10 AC 432 ms
89,084 KB
testcase_11 AC 477 ms
88,892 KB
testcase_12 AC 439 ms
88,780 KB
testcase_13 AC 426 ms
89,328 KB
testcase_14 AC 524 ms
88,924 KB
testcase_15 AC 440 ms
89,472 KB
testcase_16 AC 478 ms
89,516 KB
testcase_17 AC 478 ms
86,412 KB
testcase_18 AC 446 ms
89,096 KB
testcase_19 AC 429 ms
89,132 KB
testcase_20 AC 441 ms
89,200 KB
testcase_21 AC 415 ms
88,824 KB
testcase_22 AC 425 ms
89,056 KB
testcase_23 AC 447 ms
89,444 KB
testcase_24 AC 470 ms
89,684 KB
testcase_25 AC 455 ms
89,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main {
	
	public static void update(int[] count, String str, boolean add){
		for(final char ch : str.toCharArray()){
			count[Character.getNumericValue(ch)] += (add ? 1 : -1);
		}
	}
	
	public static boolean valid_range(int[] count, Set<Integer> valid){
		for(int i = 0; i < count.length; i++){
			if(valid.contains(i)  && count[i] <= 0){ return false; }
			if(!valid.contains(i) && count[i]  > 0){ return false; }
		}
		
		return true;
	}
	
	public static boolean less_range(int[] count, Set<Integer> valid){
		for(int i = 0; i < count.length; i++){
			if(!valid.contains(i) && count[i]  > 0){ return false; }
		}
		
		return true;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		Set<Integer> valid_set = new HashSet<Integer>();
		for(int i = 0; i < N; i++){
			valid_set.add(sc.nextInt());
		}
		
		final int MAX = 5000000;
		TreeSet<Integer> prime_tree = new TreeSet<Integer>();
		boolean[] is_prime = new boolean[MAX + 1];
		Arrays.fill(is_prime, true);
		is_prime[0] = is_prime[1] = false;
		
		for(int i = 2; i <= MAX; i++){
			if(!is_prime[i]){ continue; }
			
			prime_tree.add(i);
			for(int j = i * 2; j <= MAX; j += i){
				is_prime[j] = false;
			}
		}
		//System.out.println(valid_set);
		
		int[] count = new int[10];
		int answer = -1;
		
		int prev = 1;
		for(int start = 1; start <= MAX; start++){
			if(is_prime[start]){
				update(count, Integer.toString(start), true);
			}
			
			if(valid_range(count, valid_set)){
				//System.out.println(prev + " " + start);
				answer = Math.max(answer, start - prev);
			}else{
				if(is_prime[start] && !less_range(count, valid_set)){
					prev = start + 1;
					Arrays.fill(count, 0);
				}
			}
		}
		if(prev < MAX){
			//System.out.println(prev);
			answer = Math.max(answer, MAX - prev);
		}
		
		System.out.println(answer);
	}

}
0