結果

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

テストケース

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

ソースコード

diff #

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

using namespace std;

int get_hash(int num) {

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

	return hash;
}

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

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

	prime_check[0] = prime_check[1] = false;

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

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

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

	int max_len = 0;
	int ans = 0;

	for (int i = 0; i < primes.size(); i++) {
		int bits = 0;
		int len = 0;
		for (int j = 0; j < 10 && i + j < primes.size(); j++) {
			if ((bits & (1 << primes[i + j].second)) == 0) {
				bits |= 1 << primes[i + j].second;
				len++;
			} else {
				break;
			}
		}
		// printf("%d %d\n", primes[i].first, len);
		if (max_len <= len) {
			max_len = len;
			ans = primes[i].first;
		}
	}

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