結果

問題 No.6 使いものにならないハッシュ
ユーザー scache
提出日時 2014-11-12 21:38:52
言語 Java
(openjdk 23)
結果
AC  
実行時間 169 ms / 5,000 ms
コード長 1,290 bytes
コンパイル時間 3,705 ms
コンパイル使用メモリ 77,748 KB
実行使用メモリ 54,148 KB
最終ジャッジ日時 2024-09-16 16:22:36
合計ジャッジ時間 9,579 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #


import java.util.ArrayList;
import java.util.Scanner;

public class UselessHash {
	public static void main(String[] args) {
		UselessHash p = new UselessHash();
	}

	public UselessHash() {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		
		solve(k, n);
	}

	public void solve(int k, int n) {
		ArrayList<Integer> primes = new ArrayList<Integer>();
		boolean[] sieve = new boolean[n+1];
		
		sieve[0] = sieve[1] = true;
		for(int i=2;i<sieve.length;i++){
			if(sieve[i])
				continue;
			
			if(k<=i)
				primes.add(i);
			for(int j=i*2;j<sieve.length;j+=i){
				sieve[j] = true;
			}
		}
		
		if(primes.size() == 0){
			System.out.println(-1);
			return;
		}
		
		int res = primes.get(0);
		int curUsed = (1<<calcHash(primes.get(0)));
		int last = 0;
		int curMax = 1;
		for(int i=1;i<primes.size();i++){
			int h = calcHash(primes.get(i));
			
			while(((1<<h) & curUsed) > 0 ){
				curUsed -= (1<<calcHash(primes.get(last)));
				last++;
			}
			curUsed += (1<<h);
			
			if(curMax <= i-last+1){
				res = primes.get(last);
				curMax = i-last+1;
			}
		}
		System.out.println(res);
	}

	private int calcHash(int a){
		while(a>=10){
			a = a/10 + a%10;
		}
		
		return a;
	}
}
0