結果

問題 No.12 限定された素数
ユーザー こるこる
提出日時 2017-01-07 13:50:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 939 ms / 5,000 ms
コード長 2,462 bytes
コンパイル時間 4,754 ms
コンパイル使用メモリ 74,724 KB
実行使用メモリ 78,920 KB
最終ジャッジ日時 2023-08-15 23:36:21
合計ジャッジ時間 15,282 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 250 ms
73,412 KB
testcase_01 AC 308 ms
76,524 KB
testcase_02 AC 238 ms
73,600 KB
testcase_03 AC 254 ms
74,248 KB
testcase_04 AC 248 ms
73,684 KB
testcase_05 AC 422 ms
77,008 KB
testcase_06 AC 555 ms
77,224 KB
testcase_07 AC 668 ms
75,764 KB
testcase_08 AC 338 ms
76,684 KB
testcase_09 AC 288 ms
75,740 KB
testcase_10 AC 385 ms
76,560 KB
testcase_11 AC 913 ms
77,024 KB
testcase_12 AC 780 ms
78,920 KB
testcase_13 AC 441 ms
76,592 KB
testcase_14 AC 379 ms
77,480 KB
testcase_15 AC 458 ms
77,604 KB
testcase_16 AC 939 ms
78,528 KB
testcase_17 AC 207 ms
70,936 KB
testcase_18 AC 234 ms
73,332 KB
testcase_19 AC 235 ms
73,320 KB
testcase_20 AC 259 ms
72,804 KB
testcase_21 AC 288 ms
75,356 KB
testcase_22 AC 246 ms
73,784 KB
testcase_23 AC 241 ms
73,812 KB
testcase_24 AC 239 ms
73,448 KB
testcase_25 AC 423 ms
77,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
public class Main {
    
    static char[] a;
    
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine());
        StringTokenizer st=new StringTokenizer(br.readLine());
        a=new char[n];
        for(int i=0;i<n;i++){
            a[i]=st.nextToken().charAt(0);
        }
        
        
        int maxi=5000000;
        boolean[] e=new boolean[maxi+1];
        Arrays.fill(e,true);
        e[0]=e[1]=false;
        int i=2;
        while(i*i<=maxi){
            int j=i+i;
            while(j<=maxi){
                e[j]=false;
                j+=i;
            }i++;
            while(!e[i]){ i++; }
        }List<Integer> list=new ArrayList<>();
        for(i=0;i<maxi+1;i++){
            if(e[i]){ list.add(i); }
        }
        
        
        int sum_max=0;
        boolean end_flag=false;
        int l=0;
        int[] end_list=new int[n];
        Arrays.fill(end_list, 0);
        for(int r=0;r<list.size();r++){
            if(check(list.get(r))){
                if(sum(end_list)!=n){
                    for(int j=0;j<a.length;j++){
                        for(int k=0;k<String.valueOf(list.get(r)).length();k++){
                            if(a[j]==String.valueOf(list.get(r)).charAt(k)) end_list[j]=1;
                        }
                    }
                }if(sum(end_list)==n){
                    end_flag=true;
                    int r_index=(r==list.size()-1) ? maxi:list.get(r+1)-1;
                    int l_index=(l==0) ? 1:list.get(l-1)+1;
                    //System.out.println(r_index+" "+l_index);
                    sum_max=Math.max(sum_max,(r_index-l_index));
                }
            }else{
                Arrays.fill(end_list, 0);
                l=r+1;
            }
        }
        System.out.println((end_flag) ? sum_max:-1);
    }
    
    static boolean check(int num){
        String str=String.valueOf(num);
        int count=0;
        for(int i=0;i<str.length();i++){
            for(int j=0;j<a.length;j++){
                if(a[j]==str.charAt(i)){ count++; break; }
            }
        }
        //if(count==str.length()) System.out.println(num);
        return count==str.length();
    }
    
    static int sum(int[] array){
        int sum=0;
        for(int n:array) sum+=n;
        return sum;
    }
    
}
0