結果

問題 No.6 使いものにならないハッシュ
ユーザー pocaristpocarist
提出日時 2015-09-02 13:31:40
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 1,597 bytes
コンパイル時間 769 ms
コンパイル使用メモリ 85,196 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-25 22:08:22
合計ジャッジ時間 3,028 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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);
	ps.erase(remove_if(ps.begin(), ps.end(), [K](int x) {return x < K; }));
	vector<int> ss(ps.size());
	transform(ps.begin(), ps.end(), ss.begin(), sum_digit);
	
	for (int i = 0; i < (int)ps.size(); i++) {
		cout << ps[i] << ' ';
	}
	cout << endl;
	for (int i = 0; i < (int)ss.size(); i++) {
		cout << ss[i] << ' ';
	}
	cout << endl;

	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_len = ks.size();
				}
				break;
			} else {
				ks.insert(ss[j]);
			}
		}
		if (ans_len <= tmp_len) {
			ans_len = tmp_len;
			ans = tmp;
		}
	}
	cout << ans << endl;
	return 0;
}
0