結果

問題 No.12 限定された素数
ユーザー data9824data9824
提出日時 2015-06-11 13:47:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 76 ms / 5,000 ms
コード長 1,298 bytes
コンパイル時間 422 ms
コンパイル使用メモリ 64,788 KB
実行使用メモリ 29,080 KB
最終ジャッジ日時 2023-08-15 22:59:49
合計ジャッジ時間 3,546 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
29,080 KB
testcase_01 AC 76 ms
28,860 KB
testcase_02 AC 73 ms
28,816 KB
testcase_03 AC 67 ms
28,868 KB
testcase_04 AC 71 ms
28,880 KB
testcase_05 AC 69 ms
28,820 KB
testcase_06 AC 70 ms
28,820 KB
testcase_07 AC 71 ms
29,068 KB
testcase_08 AC 72 ms
28,944 KB
testcase_09 AC 72 ms
28,872 KB
testcase_10 AC 72 ms
29,068 KB
testcase_11 AC 69 ms
29,068 KB
testcase_12 AC 68 ms
28,996 KB
testcase_13 AC 73 ms
28,800 KB
testcase_14 AC 72 ms
29,068 KB
testcase_15 AC 72 ms
29,080 KB
testcase_16 AC 68 ms
28,920 KB
testcase_17 AC 72 ms
28,932 KB
testcase_18 AC 75 ms
29,068 KB
testcase_19 AC 75 ms
28,932 KB
testcase_20 AC 73 ms
29,000 KB
testcase_21 AC 75 ms
29,000 KB
testcase_22 AC 72 ms
28,916 KB
testcase_23 AC 71 ms
28,820 KB
testcase_24 AC 71 ms
29,068 KB
testcase_25 AC 69 ms
29,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAX_PRIME = 5000000;
bool isPrime[MAX_PRIME + 1];
vector<int> primes;
int bits[MAX_PRIME + 1] = { 0 };

int main() {
	fill(&isPrime[0], &isPrime[MAX_PRIME] + 1, true);
	isPrime[0] = false;
	isPrime[1] = false;
	for (int i = 2; i <= MAX_PRIME; ++i) {
		if (isPrime[i]) {
			for (int k = i * 2; k <= MAX_PRIME; k += i) {
				isPrime[k] = false;
			}
		}
	}
	for (int i = 2; i <= MAX_PRIME; ++i) {
		if (isPrime[i]) {
			primes.push_back(i);
		}
	}
	for (size_t i = 0; i < primes.size(); ++i) {
		int x = primes[i];
		while (x > 0) {
			int digit = x % 10;
			bits[primes[i]] |= 1 << digit;
			x /= 10;
		}
	}
	int n;
	cin >> n;
	int given = 0;
	for (int i = 0; i < n; ++i) {
		int a;
		cin >> a;
		given |= 1 << a;
	}
	int maxRange = -1;
	int first = 1;
	while (first <= MAX_PRIME) {
		for (; first <= MAX_PRIME; ++first) {
			if ((bits[first] & ~given) == 0x0) {
				break;
			}
		}
		int includedDigits = 0x0;
		int last = first;
		for (; last <= MAX_PRIME; ++last) {
			if ((bits[last] & ~given) != 0x0) {
				break;
			}
			includedDigits |= bits[last];
			if (includedDigits == given) {
				maxRange = max(maxRange, last - first);
			}
		}
		first = last + 1;
	}
	cout << maxRange << endl;
	return 0;
}
0