結果

問題 No.6 使いものにならないハッシュ
ユーザー data9824data9824
提出日時 2015-06-10 22:54:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,209 bytes
コンパイル時間 423 ms
コンパイル使用メモリ 63,988 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 22:52:19
合計ジャッジ時間 1,708 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAX_PRIME = 200100;
bool isPrime[MAX_PRIME + 1];
vector<int> primes;

int main() {
	fill(&isPrime[0], &isPrime[MAX_PRIME] + 1, true);
	isPrime[0] = false;
	isPrime[1] = false;
	for (int i = 2; i <= MAX_PRIME; ++i) {
		if (isPrime[i]) {
			for (int k = i * 2; k <= MAX_PRIME; k += i) {
				isPrime[k] = false;
			}
		}
	}
	int k, n;
	cin >> k >> n;
	for (int i = k; i <= n; ++i) {
		if (isPrime[i]) {
			primes.push_back(i);
		}
	}
	vector<int> hashes;
	for (size_t i = 0; i < primes.size(); ++i) {
		int x = primes[i];
		int hash;
		do {
			hash = 0;
			for (; x > 0; x /= 10) {
				hash += x % 10;
			}
			x = hash;
		} while (hash >= 10);
		hashes.push_back(hash);
	}
	int positions[10];
	fill(&positions[0], &positions[9] + 1, -1);
	int maxLength = 0;
	int maxFirst = 0;
	int first = 0;
	for (size_t last = 0; last < hashes.size(); ++last) {
		int h = hashes[last];
		if (positions[h] >= first) {
			first = positions[h] + 1;
		}
		positions[h] = last;
		int length = last - first + 1;
		if (maxLength <= length) {
			maxLength = length;
			maxFirst = first;
		}
	}
	cout << primes[maxFirst] << endl;
	return 0;
}
0