結果
問題 |
No.12 限定された素数
|
ユーザー |
|
提出日時 | 2016-04-12 16:28:09 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 369 ms / 5,000 ms |
コード長 | 1,372 bytes |
コンパイル時間 | 2,179 ms |
コンパイル使用メモリ | 77,188 KB |
実行使用メモリ | 48,720 KB |
最終ジャッジ日時 | 2024-11-24 08:48:52 |
合計ジャッジ時間 | 11,468 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
package yukicoder; import java.util.*; public class Main{ public static void main(String[] args)throws Exception{ new Main().solve(); } void solve(){ Scanner sc=new Scanner(System.in); int[] d=new int[10]; int n=sc.nextInt(); for(int i=0;i<n;i++){ d[sc.nextInt()]++; } boolean[] primeList=primeList(5000000); int memo[]=new int[10]; int max=-1; //k...i for(int k=1,i=k;i<=5000000&&k<=5000000;i++){ if(primeList[i]){ int a=i; while(a>=1){ memo[a%10]++; a/=10; } } if(check(d,memo)==-30){ max=Math.max(i-k,max); }else if(check(d,memo)==-20){ //継続して右側に進む }else if(check(d,memo)==-10){ while(check(d,memo)==-10){ if(primeList[k]){ int b=k; while(b>=1){ memo[b%10]--; b/=10; } } k++; } } } System.out.println(max); } boolean[] primeList(int n){ boolean[] primeList=new boolean[n+1]; Arrays.fill(primeList, true); primeList[0]=primeList[1]=false; for(int i=2;i<=n;i++){ if(primeList[i]==true){ for(int j=2;i*j<=n;j++){ primeList[i*j]=false; } } } return primeList; } int check(int[] d,int[] memo){ for(int i=0;i<10;i++){ if(d[i]==1&&memo[i]==0)return -20; if(d[i]==0&&memo[i]>0)return -10; } return -30; } void tr(Object...o){System.out.println(Arrays.deepToString(o));} }