結果

問題 No.12 限定された素数
ユーザー horizon4096horizon4096
提出日時 2018-01-11 00:32:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 409 ms / 5,000 ms
コード長 1,652 bytes
コンパイル時間 677 ms
コンパイル使用メモリ 72,604 KB
実行使用メモリ 8,672 KB
最終ジャッジ日時 2023-08-16 00:43:57
合計ジャッジ時間 11,529 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 389 ms
8,488 KB
testcase_01 AC 361 ms
8,420 KB
testcase_02 AC 387 ms
8,416 KB
testcase_03 AC 175 ms
8,524 KB
testcase_04 AC 409 ms
8,464 KB
testcase_05 AC 342 ms
8,416 KB
testcase_06 AC 345 ms
8,416 KB
testcase_07 AC 345 ms
8,488 KB
testcase_08 AC 350 ms
8,404 KB
testcase_09 AC 384 ms
8,412 KB
testcase_10 AC 340 ms
8,420 KB
testcase_11 AC 338 ms
8,420 KB
testcase_12 AC 347 ms
8,360 KB
testcase_13 AC 353 ms
8,368 KB
testcase_14 AC 349 ms
8,412 KB
testcase_15 AC 354 ms
8,468 KB
testcase_16 AC 337 ms
8,420 KB
testcase_17 AC 405 ms
8,420 KB
testcase_18 AC 388 ms
8,404 KB
testcase_19 AC 390 ms
8,412 KB
testcase_20 AC 371 ms
8,424 KB
testcase_21 AC 357 ms
8,416 KB
testcase_22 AC 404 ms
8,416 KB
testcase_23 AC 404 ms
8,672 KB
testcase_24 AC 406 ms
8,420 KB
testcase_25 AC 343 ms
8,468 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 <= 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