結果

問題 No.6 使いものにならないハッシュ
ユーザー masamasa
提出日時 2015-01-19 02:31:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,211 bytes
コンパイル時間 544 ms
コンパイル使用メモリ 69,376 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 22:48:25
合計ジャッジ時間 1,920 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>

using namespace std;

int get_hash(int num) {
	while (num > 9) {
		num = num / 10 + num % 10;
	}
	return num;
}

int main() {
	int k, n;
	cin >> k >> n;

	vector<bool> is_prime(n + 1, true);
	vector< pair<int, int> > primes;

	is_prime[0] = is_prime[1] = false;

	for (int i = 4; i <=n; i += 2) {
		is_prime[i] = false;
	}

	for (int i = 3; i * i <= n; i += 2) {
		if (is_prime[i]) {
			for (int j = i + i; j <= n; j += i) {
				is_prime[j] = false;
			}
		}
	}

	for (int i = k; i <= n; i++) {
		if (is_prime[i]) {
			primes.push_back(make_pair(i, get_hash(i)));
		}
	}

	int max_len = 0;
	int ans = 0;
	int start = 0;

	vector<int> last_used(10, -1);

	for (int end = 0; end < primes.size(); end++) {
		bool extends = false;
		int hash = primes[end].second;
		if (last_used[hash] == -1) {
			last_used[hash] = end;
			extends = true;
		} else {
			if (last_used[hash] >= start) {
				start = last_used[hash] + 1;
			}
			last_used[hash] = end;
			extends = true;
		}
		int len = end - start + 1;
		if (max_len <= len) {
			max_len = len;
			ans = primes[start].first;
		}

	}

	cout << ans << endl;
	return 0;
}
0