結果

問題 No.6 使いものにならないハッシュ
ユーザー spaciaspacia
提出日時 2015-12-30 01:35:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 100 ms / 5,000 ms
コード長 1,608 bytes
コンパイル時間 3,220 ms
コンパイル使用メモリ 74,316 KB
実行使用メモリ 52,556 KB
最終ジャッジ日時 2023-10-14 22:56:51
合計ジャッジ時間 6,410 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,304 KB
testcase_01 AC 45 ms
49,128 KB
testcase_02 AC 89 ms
51,820 KB
testcase_03 AC 63 ms
51,780 KB
testcase_04 AC 70 ms
51,988 KB
testcase_05 AC 83 ms
51,716 KB
testcase_06 AC 89 ms
51,820 KB
testcase_07 AC 86 ms
52,112 KB
testcase_08 AC 85 ms
52,128 KB
testcase_09 AC 78 ms
51,492 KB
testcase_10 AC 44 ms
49,072 KB
testcase_11 AC 66 ms
51,960 KB
testcase_12 AC 77 ms
52,136 KB
testcase_13 AC 63 ms
50,512 KB
testcase_14 AC 65 ms
51,344 KB
testcase_15 AC 89 ms
51,848 KB
testcase_16 AC 87 ms
52,400 KB
testcase_17 AC 91 ms
51,900 KB
testcase_18 AC 100 ms
52,504 KB
testcase_19 AC 93 ms
51,876 KB
testcase_20 AC 91 ms
52,104 KB
testcase_21 AC 56 ms
50,492 KB
testcase_22 AC 90 ms
51,972 KB
testcase_23 AC 90 ms
51,976 KB
testcase_24 AC 87 ms
52,032 KB
testcase_25 AC 88 ms
52,128 KB
testcase_26 AC 84 ms
51,788 KB
testcase_27 AC 79 ms
51,884 KB
testcase_28 AC 91 ms
52,556 KB
testcase_29 AC 95 ms
51,924 KB
testcase_30 AC 89 ms
51,896 KB
testcase_31 AC 91 ms
52,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.math.*;

class Main6 {
	
	public static void out (Object out) {
		System.out.println(out);
	}
	
	public static int hash (int p) {
		int ret = 0;
		while (p != 0) {
			ret += p % 10;
			p /= 10;
		}
		return ret < 10 ? ret : hash(ret);
	}
	
	public static boolean[] sieve (int n) {
		boolean[] ret = new boolean[n + 1];
		
		Arrays.fill(ret , true);
		ret[1] = false;
		for (int i = 4; i <= n; i += 2) ret[i] = false;
		for (int i = 3; i * i <= n; i += 2) {
			for (int j = 3; i * j <= n; j += 2) {
				ret[i * j] = false;
			}
		}
		return ret;
	}
	
	public static void main (String[] args) throws IOException {
		BufferedReader br = 
			new BufferedReader(new InputStreamReader(System.in));
		
		int k = Integer.parseInt(br.readLine());
		int n = Integer.parseInt(br.readLine());
		boolean[] isPrime = sieve(n);
		int[] hashes = new int[n + 1];
		
		//out("Prime---");
		for (int i = k; i <= n; i++) {
			if (!isPrime[i]) continue;
			hashes[i] = hash(i);
			//out(i + "\thash(i) = " + hashes[i]);
		}
		//out("---");
		
		int startP = 0;
		int ans = 0;
		int maxLen = 0;
		ArrayList<Integer> list = new ArrayList<Integer>();
		
		for (int i = k; i <= n; i++) {
			if (!isPrime[i]) continue;
			startP = i;
			list.add(hashes[i]);
			for (int j = i + 1; j <= n; j++) {
				if (!isPrime[j]) continue;
				if (!list.contains(hashes[j])) {
					list.add(hashes[j]);
					continue;
				}
				break;
			}
			if (maxLen <= list.size()) {
				maxLen = list.size();
				ans = startP;
				//out("ans更新 : " + ans);
			}
			list.clear();
		}
		
		out(ans);
		
	}
}
0