結果

問題 No.12 限定された素数
ユーザー data9824data9824
提出日時 2015-06-11 13:44:34
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,298 bytes
コンパイル時間 424 ms
コンパイル使用メモリ 64,424 KB
実行使用メモリ 29,084 KB
最終ジャッジ日時 2023-09-20 20:47:08
合計ジャッジ時間 4,152 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
29,000 KB
testcase_01 AC 78 ms
28,932 KB
testcase_02 AC 79 ms
29,068 KB
testcase_03 WA -
testcase_04 AC 78 ms
29,084 KB
testcase_05 AC 77 ms
28,860 KB
testcase_06 AC 78 ms
28,992 KB
testcase_07 AC 77 ms
28,804 KB
testcase_08 AC 78 ms
28,928 KB
testcase_09 AC 78 ms
29,056 KB
testcase_10 AC 79 ms
28,964 KB
testcase_11 AC 76 ms
29,044 KB
testcase_12 AC 77 ms
29,084 KB
testcase_13 AC 78 ms
28,924 KB
testcase_14 AC 79 ms
28,788 KB
testcase_15 AC 79 ms
29,020 KB
testcase_16 AC 76 ms
28,856 KB
testcase_17 AC 78 ms
29,000 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 78 ms
28,796 KB
testcase_21 AC 78 ms
29,000 KB
testcase_22 AC 78 ms
28,932 KB
testcase_23 AC 79 ms
29,020 KB
testcase_24 AC 78 ms
28,796 KB
testcase_25 AC 79 ms
28,788 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 = 0;
	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