結果

問題 No.7 プライムナンバーゲーム
ユーザー tsukiotsukio
提出日時 2016-03-22 01:26:25
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 1,437 bytes
コンパイル時間 1,009 ms
コンパイル使用メモリ 63,388 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-04-08 21:37:52
合計ジャッジ時間 4,369 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

void SetPrimeColumnInfo(pair<int, int>& pair, int start, int current) {
	int length = current - start;
	if (pair.second <= length) {
		pair.first = start;
		pair.second = length;
	}
}

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

	vector<int> search_nums(n + 1, 1);
	search_nums[0] = 0;
	search_nums[1] = 0;
	
	for (int i = 2; i < search_nums.size(); i++) {
		if (search_nums[i] == 1) {
			for (int j = i * 2; j < search_nums.size(); j += i) {
				search_nums[j] = 0;
			}
		}
	}

	vector<int> primes;
	for (int i = k; i <= n; i++) {
		if (search_nums[i] == 1) {
			primes.emplace_back(i);
		}
	}

	vector<int> hash_nums;
	for (int prime : primes) {
		int num = 0;
		while (prime > 0) {
			num += prime % 10;
			prime /= 10;
			if (num >= 10) {
				num = (num / 10) + (num % 10);
			}
		}

		hash_nums.emplace_back(num);
	}

	pair<int, int> result(0, 0);

	int start = 0;
	for (int current = 1; current < hash_nums.size(); current++)
	{
		bool same_exist = false;
		int check_pos = start;
		for (; check_pos < current; check_pos++) {
			if (hash_nums[check_pos] == hash_nums[current]) {
				same_exist = true;
				break;
			}
		}

		if (same_exist) {
			SetPrimeColumnInfo(result, start, current);
			start = check_pos + 1;
		}
		if (current == hash_nums.size() - 1) {
			SetPrimeColumnInfo(result, start, current + 1);
		}
	}

	cout << primes[result.first] << endl;

	return 0;
}
0