結果

問題 No.12 限定された素数
ユーザー core123core123
提出日時 2019-04-18 16:48:18
言語 Java21
(openjdk 21)
結果
AC  
実行時間 564 ms / 5,000 ms
コード長 2,101 bytes
コンパイル時間 3,000 ms
コンパイル使用メモリ 81,804 KB
実行使用メモリ 82,620 KB
最終ジャッジ日時 2023-08-16 00:59:46
合計ジャッジ時間 16,390 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 422 ms
79,848 KB
testcase_01 AC 502 ms
81,472 KB
testcase_02 AC 415 ms
79,752 KB
testcase_03 AC 457 ms
77,568 KB
testcase_04 AC 425 ms
79,856 KB
testcase_05 AC 460 ms
80,984 KB
testcase_06 AC 462 ms
81,040 KB
testcase_07 AC 468 ms
81,240 KB
testcase_08 AC 464 ms
80,440 KB
testcase_09 AC 477 ms
81,684 KB
testcase_10 AC 487 ms
80,452 KB
testcase_11 AC 564 ms
81,828 KB
testcase_12 AC 523 ms
82,620 KB
testcase_13 AC 527 ms
81,776 KB
testcase_14 AC 511 ms
82,288 KB
testcase_15 AC 514 ms
81,928 KB
testcase_16 AC 472 ms
80,804 KB
testcase_17 AC 378 ms
75,992 KB
testcase_18 AC 422 ms
79,708 KB
testcase_19 AC 419 ms
77,332 KB
testcase_20 AC 424 ms
79,768 KB
testcase_21 AC 431 ms
80,840 KB
testcase_22 AC 424 ms
80,024 KB
testcase_23 AC 411 ms
79,788 KB
testcase_24 AC 410 ms
80,328 KB
testcase_25 AC 460 ms
81,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.util.stream.*;
import static java.lang.Math.*;
public class Main{
	public static void main(String... args){
		Scanner sc=new Scanner(System.in);
		boolean[] isPrime=new boolean[5000001];
		Arrays.fill(isPrime,true);
		isPrime[0]=false;
		isPrime[1]=false;
		for(int i=2;i<isPrime.length;i++){
			for(int j=2*i;j<isPrime.length;j+=i){
				isPrime[j]=false;
			}
		}
		List<Integer> primes=new ArrayList<>();
		for(int i=0;i<isPrime.length;i++){
			if(isPrime[i])primes.add(i);
		}
		
		
		int n=sc.nextInt();
		boolean[] canUses=new boolean[10];
		for(int i=0;i<n;i++){
			canUses[sc.nextInt()]=true;
		}
		int max=-1;
		main:for(int i=0;i<primes.size();i++){
			if(!canUse(primes.get(i),canUses))continue;
			Set<Integer> set=new HashSet<>();
			int j=i;
			while(j<primes.size()&&canUse(primes.get(j),canUses)){
				for(char c:Integer.toString(primes.get(j)).toCharArray()){
					set.add(c-'0');
				}
				j++;
			}
			//put("(i,j)=(%d,%d)",i,j);
			for(int k=0;k<10;k++){
				if(!canUses[k])continue;
				if(!set.contains(k)){
					i=j;
					continue main;
				}
			}
			int l=i-1<0?1:primes.get(i-1)+1;
			int r=j>=primes.size()?5000000:primes.get(j)-1;
			//put("(l,r)=(%d,%d)",l,r);
			max=max(max,r-l);
			i=j;
		}
		put(max);
		
	}
	public static boolean canUse(int x,boolean[] canUses){
		for(char c:Integer.toString(x).toCharArray()){
			if(!canUses[c-'0'])return false;
		}
		return true;
	}
	
	public static class Pair{
		final int x,y;
		Pair(int x,int y){
			this.x=x;
			this.y=y;
		}
		@Override
		public String toString(){
			return String.format("Pair(%d,%d)",x,y);
		}
	}
	public static void print(Object object){
        System.out.print(object);
    }
    public static void put(Object object) {
        System.out.println(object);
    }
    public static void put(){
        System.out.println();
    }
 
    public static void print(String format,Object... args){
        System.out.print(String.format(format,args));
    }
    public static void put(String format,Object... args) {
        System.out.println(String.format(format,args));
    }
}
0