結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 3 ms
4,348 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,348 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 2 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,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 3 ms
4,352 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 3 ms
4,352 KB
testcase_21 AC 1 ms
4,352 KB
testcase_22 AC 2 ms
4,352 KB
testcase_23 AC 2 ms
4,352 KB
testcase_24 AC 2 ms
4,352 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 2 ms
4,348 KB
testcase_29 AC 3 ms
4,352 KB
testcase_30 AC 3 ms
4,352 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;
}

string strbits(int num) {
	string str;
	for (int i = 9; i >= 0; i--) {
		str += (num & (1 << i)) ? "1" : "0";
	}
	return str;
}

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 bits = 0;
	int start = 0;
	int end = 0;

	for (int end = 0; end < primes.size(); end++) {
		if (bits & (1 << primes[end].second)) {
			while (bits & (1 << primes[end].second)) {
				bits &= ~(1 << primes[start].second);
				start++;
			}
		}
		bits |= (1 << primes[end].second);
		if (max_len <= end - start + 1) {
			max_len = end - start + 1;
			ans = primes[start].first;
		}
	}

	if (max_len <= end - start + 1) {
		max_len = end - start + 1;
		ans = primes[start].first;
	}

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