結果

問題 No.12 限定された素数
ユーザー kou6839kou6839
提出日時 2014-11-11 16:10:20
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 1,309 bytes
コンパイル時間 2,723 ms
コンパイル使用メモリ 80,196 KB
実行使用メモリ 155,508 KB
最終ジャッジ日時 2024-12-31 09:42:26
合計ジャッジ時間 21,456 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
 

public class Main {
	static boolean finalcheck(String a,String b){
		for(int i=0;i<b.length();i++ ){
			if( !a.contains(b.charAt(i)+"") ) return false;
		}
		return true;
	}
	static boolean check(String a,String b){
		for(int i=0;i<a.length();i++ ){
			if( !b.contains(a.charAt(i)+"") ) return false;
		}
		return true;
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		String a = "";
		for(int i=0;i<N;i++){
			int temp=sc.nextInt();
			a+=temp;
		}
		ArrayList<Integer> sosuu = new ArrayList<>();
		sosuu.add(0);
		boolean[] era = new boolean[5000001];
		for(int i=2;i<=5000000;i++){
			if(!era[i]){
				sosuu.add(i);
				for(int j=2*i;j<=5000000;j+=i){
					era[j]=true;
				}
			}
		}
		int t=1;
		int s=0;
		int ans=0;
		int size = sosuu.size();
		String go="";
		while(t<size){
			if(check(sosuu.get(t)+"",a)){
				if(t==size-1){
					ans=Math.max(ans, 5000000-(sosuu.get(s)+1));
					break;
				}else{
					go+=sosuu.get(t);
				}
			}else{
				if(finalcheck(go,a)){
					ans=Math.max(ans, (sosuu.get(t)-1)-(sosuu.get(s)+1));
				}
				s=t;
				go="";
			}
			t++;
		}
		if(ans==0){
			System.out.println(-1);
		}else{
			System.out.println(ans);
		}
	}
}	
0