結果

問題 No.12 限定された素数
ユーザー uwiuwi
提出日時 2015-02-18 22:04:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 219 ms / 5,000 ms
コード長 5,224 bytes
コンパイル時間 4,200 ms
コンパイル使用メモリ 88,208 KB
実行使用メモリ 54,208 KB
最終ジャッジ日時 2024-05-03 09:08:51
合計ジャッジ時間 10,883 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 201 ms
53,980 KB
testcase_01 AC 208 ms
53,956 KB
testcase_02 AC 209 ms
53,780 KB
testcase_03 AC 170 ms
53,928 KB
testcase_04 AC 217 ms
53,896 KB
testcase_05 AC 212 ms
53,788 KB
testcase_06 AC 214 ms
54,044 KB
testcase_07 AC 209 ms
54,036 KB
testcase_08 AC 205 ms
53,780 KB
testcase_09 AC 199 ms
53,976 KB
testcase_10 AC 214 ms
53,676 KB
testcase_11 AC 216 ms
54,016 KB
testcase_12 AC 207 ms
53,748 KB
testcase_13 AC 219 ms
53,676 KB
testcase_14 AC 212 ms
54,008 KB
testcase_15 AC 219 ms
53,908 KB
testcase_16 AC 215 ms
53,848 KB
testcase_17 AC 195 ms
53,704 KB
testcase_18 AC 193 ms
53,764 KB
testcase_19 AC 198 ms
53,860 KB
testcase_20 AC 204 ms
53,980 KB
testcase_21 AC 210 ms
53,912 KB
testcase_22 AC 199 ms
53,908 KB
testcase_23 AC 203 ms
53,672 KB
testcase_24 AC 198 ms
53,904 KB
testcase_25 AC 209 ms
54,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)); }
}
0