結果
| 問題 |
No.12 限定された素数
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-02-18 22:04:34 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 241 ms / 5,000 ms |
| コード長 | 5,224 bytes |
| コンパイル時間 | 4,087 ms |
| コンパイル使用メモリ | 83,840 KB |
| 実行使用メモリ | 41,440 KB |
| 最終ジャッジ日時 | 2024-11-24 07:52:48 |
| 合計ジャッジ時間 | 10,872 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
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)); }
}