結果
問題 | No.12 限定された素数 |
ユーザー | uwi |
提出日時 | 2015-02-18 22:02:18 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 342 ms / 5,000 ms |
コード長 | 5,197 bytes |
コンパイル時間 | 4,247 ms |
コンパイル使用メモリ | 83,764 KB |
実行使用メモリ | 41,504 KB |
最終ジャッジ日時 | 2024-05-03 09:07:37 |
合計ジャッジ時間 | 12,688 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 284 ms
41,376 KB |
testcase_01 | AC | 325 ms
41,316 KB |
testcase_02 | AC | 283 ms
40,976 KB |
testcase_03 | AC | 239 ms
41,412 KB |
testcase_04 | AC | 318 ms
41,332 KB |
testcase_05 | AC | 290 ms
41,492 KB |
testcase_06 | AC | 328 ms
41,476 KB |
testcase_07 | AC | 316 ms
41,352 KB |
testcase_08 | AC | 288 ms
41,344 KB |
testcase_09 | AC | 291 ms
41,108 KB |
testcase_10 | AC | 323 ms
41,116 KB |
testcase_11 | AC | 280 ms
41,352 KB |
testcase_12 | AC | 317 ms
41,280 KB |
testcase_13 | AC | 273 ms
41,112 KB |
testcase_14 | AC | 321 ms
41,340 KB |
testcase_15 | AC | 275 ms
41,400 KB |
testcase_16 | AC | 342 ms
41,504 KB |
testcase_17 | AC | 276 ms
41,368 KB |
testcase_18 | AC | 283 ms
41,308 KB |
testcase_19 | AC | 288 ms
41,252 KB |
testcase_20 | AC | 313 ms
41,356 KB |
testcase_21 | AC | 293 ms
41,232 KB |
testcase_22 | AC | 298 ms
41,140 KB |
testcase_23 | AC | 281 ms
40,908 KB |
testcase_24 | AC | 292 ms
41,016 KB |
testcase_25 | AC | 283 ms
41,496 KB |
ソースコード
package q0xx; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Q34 { static InputStream is; static PrintWriter out; static String INPUT = ""; static void solve() { long[] isp = isp(5000000); int n = ni(); int ptn = 0; for(int i = 0;i < n;i++){ ptn |= 1<<ni(); } int[] f = new int[10]; int fptn = 0; int tail = 1; int maxlen = -1; for(int head = 1;head <= 5000000;head++){ if(isp[head>>>6]<<~head<0){ int x = head; while(x > 0){ if(++f[x%10] == 1)fptn ^= 1<<x%10; x /= 10; } } for(int j = 0;j <= 9;j++){ if(f[j] > 0 && ptn<<~j>=0){ if(isp[tail>>>6]<<~tail<0){ int x = tail; while(x > 0){ if(--f[x%10] == 0)fptn ^= 1<<x%10; x /= 10; } } tail++; j--; continue; } } if(fptn == ptn && head-tail > maxlen){ maxlen = head-tail; } } out.println(maxlen); } public static long[] isp(int n) { int[] tprimes = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61}; if(n <= 64){ long ptn = 0; for(int p : tprimes)if(p <= n)ptn |= 1L<<p; return new long[]{ptn}; } long[] isnp = new long[(n+1)/64+1]; int sup = (n+1)/64+1; isnp[0] |= 1<<1; for(int tp : tprimes){ long[] ptn = new long[tp]; for(int i = 0;i < tp<<6;i+=tp)ptn[i>>>6] |= 1L<<i; for(int j = 0;j < sup;j += tp){ for(int i = 0;i < tp && i+j < sup;i++){ isnp[j+i] |= ptn[i]; } } } final int[] magic = {0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28, 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11, 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10, 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12}; for(int i = 0;i < sup;i++){ for(long j = ~isnp[i];j != 0;j &= j-1){ int p = i<<6|magic[(int)((j&-j)*0x022fdd63cc95386dL>>>58)]; if((long)p*p > n)break; for(int q = p*p;q <= n;q += p)isnp[q>>6] |= 1L<<q; } } for(int i = 0;i < isnp.length;i++)isnp[i] = ~isnp[i]; for(int tp : tprimes)isnp[0] |= 1L<<tp; isnp[isnp.length-1] &= (1L<<n+1)-1; return isnp; } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); long G = System.currentTimeMillis(); tr(G-S+"ms"); } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double nd() { return Double.parseDouble(ns()); } private static char nc() { return (char)skip(); } private static String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private static int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private static int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }