結果

問題 No.12 限定された素数
ユーザー horizon4096horizon4096
提出日時 2018-01-11 00:36:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 385 ms / 5,000 ms
コード長 1,656 bytes
コンパイル時間 841 ms
コンパイル使用メモリ 72,084 KB
実行使用メモリ 8,552 KB
最終ジャッジ日時 2023-08-16 00:43:45
合計ジャッジ時間 11,091 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 379 ms
8,444 KB
testcase_01 AC 350 ms
8,416 KB
testcase_02 AC 369 ms
8,428 KB
testcase_03 AC 172 ms
8,416 KB
testcase_04 AC 382 ms
8,508 KB
testcase_05 AC 331 ms
8,412 KB
testcase_06 AC 334 ms
8,548 KB
testcase_07 AC 336 ms
8,404 KB
testcase_08 AC 335 ms
8,412 KB
testcase_09 AC 364 ms
8,412 KB
testcase_10 AC 324 ms
8,476 KB
testcase_11 AC 326 ms
8,404 KB
testcase_12 AC 336 ms
8,404 KB
testcase_13 AC 343 ms
8,484 KB
testcase_14 AC 333 ms
8,508 KB
testcase_15 AC 342 ms
8,548 KB
testcase_16 AC 331 ms
8,412 KB
testcase_17 AC 385 ms
8,464 KB
testcase_18 AC 367 ms
8,416 KB
testcase_19 AC 357 ms
8,428 KB
testcase_20 AC 352 ms
8,420 KB
testcase_21 AC 335 ms
8,552 KB
testcase_22 AC 359 ms
8,412 KB
testcase_23 AC 356 ms
8,476 KB
testcase_24 AC 370 ms
8,344 KB
testcase_25 AC 317 ms
8,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <algorithm>
#include <numeric>
#include <utility>
#include <cmath>

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;

const int iinf = 1 << 29;
const long long linf = 1ll << 61;

#define dump(x) cerr << #x << " : " << x << '\n'
#define MOD(x, y) (((y) + (x) % (y)) % (y))

int N;
bool A[10];
bool erat[5000001];

int isused(int n, int d) {
	while (n != 0) {
		if (n % 10 == d) return 1;
		n /= 10;
	}
	return 0;
}

int main(int argc, char* argv[])
{
	scanf("%d", &N);
	for (int i = 0; i < N; i++) {
		int a;
		scanf("%d", &a);
		A[a] = true;
	}
	fill(erat + 2, erat + 5000001, true);
	for (int i = 2; i * i <= 5000000; i++) {
		if (!erat[i]) continue;
		for (int j = 2 * i; j <= 5000000; j += i) {
			erat[j] = false;
		}
	}

	int used[10] = {};
	int k = 1, l = 1;
	int ans = -1;
	while (true) {
		while (true) {
			bool f1 = true;
			for (int i = 0; i < 10; i++) {
				if ((used[i] > 0) && !A[i]) f1 = false;
			}
			bool f2 = true;
			for (int i = 0; i < 10; i++) {
				if ((used[i] == 0) && A[i]) f2 = false;
			}
			if (f1 & f2) {
				ans = max(ans, l - k);
				break;
			} else if (!f2) {
				break;
			}
			if (erat[k]) {
				for (int i = 0; i < 10; i++) {
					used[i] -= isused(k, i);
				}
			}
			k++;
		}
		l++;
		if (l > 5000000) break;
		if (erat[l]) {
			for (int i = 0; i < 10; i++) {
				used[i] += isused(l, i);
			}
		}
	}
	printf("%d\n", ans);

	return 0;
}
0