結果

問題 No.6 使いものにならないハッシュ
ユーザー plasma_eplasma_e
提出日時 2019-01-17 17:40:18
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 10 ms / 5,000 ms
コード長 1,222 bytes
コンパイル時間 807 ms
コンパイル使用メモリ 94,808 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-14 23:13:03
合計ジャッジ時間 2,155 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<set>

int hash(int v)
{
	if (v < 10)
	{
		return v;
	}
	else
	{
		int n = 0;
		while (v != 0)
		{
			n += v % 10;
			v /= 10;
		}
		return hash(n);
	}
}

int main()
{
	int K, N;
	std::cin >> K >> N;
	std::vector<int> primes = { 2,3,5,7 };
	for (int i = 11; i <= N; ++i)
	{
		for (auto p : primes)
		{
			if (p*p > i)
			{
				break;
			}
			else if (i%p == 0)
			{
				goto next;
			}
		}
		primes.emplace_back(i);
	next:;
	}
	std::vector<int> hashes(primes.size());
	for (std::size_t i = 0; i < primes.size(); ++i)
	{
		hashes[i] = hash(primes[i]);
	}
	int maxlength = 0;
	int maxstart = 0;
	int nowstart = std::lower_bound(primes.begin(), primes.end(), K) - primes.begin();
	std::vector<int> smallset(10);
	int count = 0;
	for (std::size_t i = nowstart; i < primes.size(); ++i)
	{
		auto h = hashes[i];
		while (smallset[h])
		{
			smallset[hashes[nowstart]] = false;
			++nowstart;
			--count;
		}
		smallset[h] = true;
		++count;
		if (maxlength <= count)
		{
			maxstart = nowstart;
			maxlength = count;
		}
	}
	std::cout << primes[maxstart] << std::endl;
}
0