結果

問題 No.12 限定された素数
ユーザー kou6839
提出日時 2014-11-11 14:49:22
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,143 bytes
コンパイル時間 2,612 ms
コンパイル使用メモリ 84,216 KB
実行使用メモリ 75,764 KB
最終ジャッジ日時 2024-12-31 09:40:26
合計ジャッジ時間 13,246 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 9 WA * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
 

public class Main {
	static boolean check(int a,String b){
		String c=a+"";
		for(int i=0;i<b.length();i++ ){
			if( !c.contains(b.charAt(i)+"") ) return false;
		}
		for(int i=0;i<c.length();i++){
			if( !b.contains(c.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<>();
		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=0;
		int s=0;
		int ans=0;
		int size = sosuu.size();
		while(t<size){
			if(check(sosuu.get(t),a)){
				if(!(t==size-1)){
					ans=Math.max(ans, sosuu.get(t+1)-1-(sosuu.get(s)+1));
				}
				else{
					ans=Math.max(ans, 500000-sosuu.get(s)+1);
				}
			}else{
				s=t;
			}
			t++;			
		}
		if(ans==0){
			System.out.println(-1);
		}else{
			System.out.println(ans);
		}
	}
}	
0