結果

問題 No.6 使いものにならないハッシュ
ユーザー pocaristpocarist
提出日時 2015-09-02 13:43:01
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 1,504 bytes
コンパイル時間 791 ms
コンパイル使用メモリ 84,888 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 22:55:22
合計ジャッジ時間 2,102 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <queue>
#include <vector>
#include <tuple>
#include <algorithm>
#include <cmath>
#include <set>

using namespace std;

vector<int> primes(int n)
{
	vector<bool> table(n + 1, true);
	table[0] = table[1] = false;
	int mid = (int)floor(sqrt(n));
	for (int i = 2; i <= mid; i++) {
		if (table[i]) {
			for (int j = i+i; j <= n; j += i)
				table[j] = false;
		}
	}
	vector<int> ans;
	for (int i = 2; i <= n; i++)
		if (table[i])
			ans.push_back(i);
	return ans;
}

int sum_digit(int n)
{
	int i = n;
	int acc = 0;
	for (;;) {
		int m = i % 10;
		int d = i / 10;
		acc += m;
		if (d == 0) {
			if (acc < 10) {
				break;
			} else {
				i = acc;
				acc = 0;
			}
		} else {
			i = d;
		}
	}
	return acc;
}

int main()
{
	int K, N;
	cin >> K >> N;
	vector<int> ps = primes(N);
	auto it = remove_if(ps.begin(), ps.end(), [K](int x) {return x < K; });
	if (it != ps.end())
		ps.erase(it);
	vector<int> ss(ps.size());
	transform(ps.begin(), ps.end(), ss.begin(), sum_digit);
	
	int len = ps.size();
	int ans = 0;
	int ans_len = 0;
	for (int i = 0; i < len; i++) {
		set<int> ks;
		ks.insert(ss[i]);
		int tmp = ps[i];
		int tmp_len = 1;
		for (int j = i+1; j < len; j++) {
			if (ks.find(ss[j]) != ks.end()) {
				if (tmp_len <= (int)ks.size()) {
//					tmp = ps[i];
					tmp_len = ks.size();
				}
				break;
			} else {
				ks.insert(ss[j]);
			}
		}
		tmp_len = ks.size();
		if (ans_len <= tmp_len) {
			ans_len = tmp_len;
			ans = tmp;
		}
	}
	cout << ans << endl;
	return 0;
}
0