結果

問題 No.6 使いものにならないハッシュ
ユーザー uafr_csuafr_cs
提出日時 2015-06-09 03:39:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 186 ms / 5,000 ms
コード長 1,922 bytes
コンパイル時間 4,008 ms
コンパイル使用メモリ 85,584 KB
実行使用メモリ 61,356 KB
最終ジャッジ日時 2023-10-14 22:52:46
合計ジャッジ時間 8,752 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
56,084 KB
testcase_01 AC 119 ms
56,716 KB
testcase_02 AC 186 ms
60,108 KB
testcase_03 AC 159 ms
57,412 KB
testcase_04 AC 162 ms
58,844 KB
testcase_05 AC 171 ms
58,948 KB
testcase_06 AC 169 ms
57,968 KB
testcase_07 AC 171 ms
58,364 KB
testcase_08 AC 174 ms
58,380 KB
testcase_09 AC 161 ms
56,716 KB
testcase_10 AC 116 ms
55,800 KB
testcase_11 AC 162 ms
57,252 KB
testcase_12 AC 175 ms
59,060 KB
testcase_13 AC 147 ms
56,000 KB
testcase_14 AC 163 ms
56,744 KB
testcase_15 AC 171 ms
58,076 KB
testcase_16 AC 174 ms
59,056 KB
testcase_17 AC 183 ms
58,400 KB
testcase_18 AC 182 ms
59,612 KB
testcase_19 AC 184 ms
58,700 KB
testcase_20 AC 182 ms
58,764 KB
testcase_21 AC 160 ms
57,044 KB
testcase_22 AC 178 ms
58,740 KB
testcase_23 AC 177 ms
58,764 KB
testcase_24 AC 178 ms
58,768 KB
testcase_25 AC 164 ms
58,292 KB
testcase_26 AC 179 ms
61,356 KB
testcase_27 AC 173 ms
58,220 KB
testcase_28 AC 174 ms
59,028 KB
testcase_29 AC 186 ms
58,728 KB
testcase_30 AC 179 ms
59,268 KB
testcase_31 AC 176 ms
58,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main {
	
	public static final int hash(int n){
		if(n < 10){
			return n;
		}else{
			int ret = 0;
			for(final char c : (n + "").toCharArray()){
				ret += Character.getNumericValue(c);
			}
			
			return hash(ret);
		}
	}
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		final int K = sc.nextInt();
		final int N = sc.nextInt();
		
		boolean[] is_prime = new boolean[N + 1];
		Arrays.fill(is_prime, true);
		is_prime[0] = is_prime[1] = false;
		LinkedList<Integer> primes = new LinkedList<Integer>();
		for(int p = 2; p <= N; p++){
			if(is_prime[p]){
				if(p >= K){
					primes.add(p);
				}
				
				for(int j = 2 * p; j <= N; j += p){
					is_prime[j] = false;
				}
			}
		}
		
		boolean[] hash_use = new boolean[10];
		LinkedList<Integer> hash_using = new LinkedList<Integer>();
		LinkedList<Integer> prime_using = new LinkedList<Integer>();
		
		int max_len = 1, min = Integer.MAX_VALUE;
		for(final int prime : primes){
			final int hash = hash(prime);
			
			if(hash_use[hash]){
				while(true){
					final int removed_prime = prime_using.poll();
					if(max_len <= hash_using.size()){
						max_len = hash_using.size();
						min = removed_prime;
					}
					
					final int removed_hash = hash_using.poll();
					hash_use[removed_hash] = false;

					
					if(removed_hash == hash){
						break;
					}
				}
			}
			
			hash_using.add(hash);
			prime_using.add(prime);
			hash_use[hash] = true;
			
			//System.out.println(hash + " : " + hash_using + "="  + prime_using + " " + prime + " " + min);
		}
		
		if(!hash_using.isEmpty() && max_len <= hash_using.size()){
			max_len = hash_using.size();
			min = prime_using.getFirst();
		}
		
		
		System.out.println(min);
	}
	
}
0