結果

問題 No.6 使いものにならないハッシュ
ユーザー tenten
提出日時 2020-11-26 18:30:55
言語 Java
(openjdk 23)
結果
AC  
実行時間 181 ms / 5,000 ms
コード長 1,316 bytes
コンパイル時間 2,475 ms
コンパイル使用メモリ 77,928 KB
実行使用メモリ 43,052 KB
最終ジャッジ日時 2024-09-16 17:00:04
合計ジャッジ時間 8,858 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		HashSet<Integer> used = new HashSet<>();
		ArrayDeque<Integer> deq = new ArrayDeque<>();
		int max = 0;
		int ans = 0;
		boolean[] isNotPrime = new boolean[n + 1];
		for (int i = 2; i <= n; i++) {
		    if (!isNotPrime[i]) {
    	        for (int j = 2; j * i <= n; j++) {
    	            isNotPrime[j * i] = true;
    	        }
    	        if (i < k) {
    	            continue;
    	        }
		        int h = hash(i);
		        if (used.contains(h)) {
		            while (deq.size() > 0) {
		                int x = deq.poll();
		                if (hash(x) == h) {
		                    break;
		                }
		                used.remove(hash(x));
		            }
		        }
		        used.add(h);
    	        deq.add(i);
    	        if (max <= deq.size()) {
    	            max = deq.size();
    	            ans = deq.getFirst();
    	        }
		    }
		}
        System.out.println(ans);
	}
	
	static int hash(int x) {
	    if (x < 10) {
	        return x;
	    } else {
	        int sum = 0;
	        while (x > 0) {
	            sum += x % 10;
	            x /= 10;
	        }
	        return hash(sum);
	    }
	}
}

0