結果
| 問題 | No.12 限定された素数 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2014-11-12 03:33:35 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 270 ms / 5,000 ms | 
| コード長 | 1,413 bytes | 
| コンパイル時間 | 2,683 ms | 
| コンパイル使用メモリ | 79,264 KB | 
| 実行使用メモリ | 55,572 KB | 
| 最終ジャッジ日時 | 2024-11-24 07:34:07 | 
| 合計ジャッジ時間 | 10,418 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 26 | 
ソースコード
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Main p = new Main();
	}
	public Main() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		for(int i=0;i<n;i++){
			a[i] =sc.nextInt();
		}
		solve(a);
	}
	public void solve(int[] a) {		
		int target = 0;
		for(int i=0;i<a.length;i++){
			target |= (1<<a[i]);
		}
		
		boolean[] hurui = new boolean[5000001];
		Arrays.fill(hurui, 2, hurui.length, true);
		ArrayList<Integer> primeList = new ArrayList<Integer>(); 
		primeList.add(0);
		for(int i=2;i<hurui.length;i++){
			if(hurui[i]){
				primeList.add(i);
				for(int j=i*2;j<hurui.length;j+=i){
					hurui[j] = false;
				}
			}
		}
		primeList.add(5000001);
		
		int cur = 0;		
		int res = 0;
		int low = 1;
		int high = 1;
		
		for(int i=1;i<primeList.size()-1;i++){
			int curNum = primeList.get(i);
			int curShift = 0;
			while(curNum>0){
				curShift |= (1<<(curNum%10));
				curNum/=10;
			}
			
			cur |= curShift;
			high = i;
			
			if(target == cur){
				res = Math.max(res, primeList.get(high+1)-primeList.get(low-1)-2);
			}else if((target|cur)-target > 0){
				low = high = i+1;
				cur = 0;
			}
		}
		
		if(res==0)
			System.out.println(-1);
		else
			System.out.println(res);
		
	}
}
            
            
            
        