結果

問題 No.6 使いものにならないハッシュ
ユーザー ゴリポン先生ゴリポン先生
提出日時 2023-09-13 09:22:35
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 1,585 bytes
コンパイル時間 4,414 ms
コンパイル使用メモリ 162,920 KB
実行使用メモリ 5,916 KB
最終ジャッジ日時 2023-09-13 09:22:42
合計ジャッジ時間 6,301 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 14 ms
4,572 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 5 ms
4,384 KB
testcase_06 AC 8 ms
5,916 KB
testcase_07 AC 5 ms
4,380 KB
testcase_08 AC 7 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 4 ms
4,380 KB
testcase_12 AC 10 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 8 ms
4,380 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 10 ms
4,376 KB
testcase_18 AC 13 ms
4,600 KB
testcase_19 AC 9 ms
4,380 KB
testcase_20 AC 9 ms
4,380 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 9 ms
4,380 KB
testcase_23 AC 9 ms
4,380 KB
testcase_24 AC 9 ms
4,376 KB
testcase_25 AC 6 ms
5,852 KB
testcase_26 AC 11 ms
4,380 KB
testcase_27 AC 7 ms
4,376 KB
testcase_28 AC 6 ms
4,380 KB
testcase_29 AC 11 ms
4,380 KB
testcase_30 AC 10 ms
4,376 KB
testcase_31 AC 9 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

module main;

import std;
// https://qiita.com/drken/items/ecd1a472d3a0e7db8dce より
bool[] isPrime, isPrimeSmall;
// K以上N以下の素数とそのハッシュを計算して入れる
int[] primes, hashes;
// [a, b)の整数に対して篩をかける。isPrime[i - a] == true ⇔ iが素数
void segmentSieve(int a, int b)
{
	for (int i = 0; i * i < b; i++) isPrimeSmall[i] = true;
	isPrime[0 .. b - a] = true;

	for (int i = 2; i * i < b; i++) {
		if (!isPrimeSmall[i]) continue;
		// [2, √b)の篩
		for (int j = 2 * i; j * j < b; j += i) isPrimeSmall[j] = false;
		// [a, b)の篩
		for (int j = max(2, (a + i - 1) / i) * i; j < b; j += i) isPrime[j - a] = false;
	}
}
// 整数の各桁を1桁になるまで足す
int calcHash(int x)
{
	while (x >= 10) {
		x = x.to!string.map!"a-'0'".sum;
	}
	return x;
}
void main()
{
	// 入力
	int K = readln.chomp.to!int;
	int N = readln.chomp.to!int;
	isPrime = new bool[](N + 2);
	isPrimeSmall = new bool[](450);	// √200_000 = 447.213…
	// 答えの計算と出力
	segmentSieve(K, N + 1);
	foreach (i; K .. N + 1) {
		if (!isPrime[i - K] || i == 1) continue;
		// iとiのハッシュを配列に追加
		primes ~= i;
		hashes ~= calcHash(i);
	}
	// しゃくとり法
	int r = 0, maxLen = 1, ans, len = cast(int)hashes.length;
	int[int] hashList;
	foreach (l; 0 .. len) {
		while (r < len && (hashes[r] !in hashList || hashList[hashes[r]] <= 0))
			hashList[hashes[r++]]++;
		if (maxLen <= r - l + 1) {
			ans = l;
			maxLen = r - l + 1;
		}
		if (r == l)
			r++;
		else
			hashList[hashes[l]]--;
	}
	writeln(primes[ans]);
}
0