結果

問題 No.6 使いものにならないハッシュ
ユーザー spacia
提出日時 2015-12-30 01:35:11
言語 Java
(openjdk 23)
結果
AC  
実行時間 98 ms / 5,000 ms
コード長 1,608 bytes
コンパイル時間 2,352 ms
コンパイル使用メモリ 78,100 KB
実行使用メモリ 39,336 KB
最終ジャッジ日時 2024-09-16 16:31:14
合計ジャッジ時間 6,130 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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