結果

問題 No.12 限定された素数
ユーザー yuustiyuusti
提出日時 2015-02-18 20:39:58
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 295 ms / 5,000 ms
コード長 1,647 bytes
コンパイル時間 2,306 ms
コンパイル使用メモリ 85,688 KB
実行使用メモリ 23,184 KB
最終ジャッジ日時 2023-08-15 22:41:13
合計ジャッジ時間 7,389 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 186 ms
22,912 KB
testcase_01 AC 189 ms
22,896 KB
testcase_02 AC 160 ms
22,904 KB
testcase_03 AC 295 ms
23,104 KB
testcase_04 AC 184 ms
22,912 KB
testcase_05 AC 180 ms
22,960 KB
testcase_06 AC 175 ms
22,972 KB
testcase_07 AC 182 ms
22,916 KB
testcase_08 AC 177 ms
22,912 KB
testcase_09 AC 183 ms
22,968 KB
testcase_10 AC 177 ms
22,964 KB
testcase_11 AC 165 ms
23,180 KB
testcase_12 AC 194 ms
22,908 KB
testcase_13 AC 204 ms
23,184 KB
testcase_14 AC 207 ms
23,176 KB
testcase_15 AC 195 ms
22,976 KB
testcase_16 AC 197 ms
22,964 KB
testcase_17 AC 210 ms
22,916 KB
testcase_18 AC 168 ms
22,912 KB
testcase_19 AC 166 ms
23,036 KB
testcase_20 AC 158 ms
23,036 KB
testcase_21 AC 187 ms
23,176 KB
testcase_22 AC 185 ms
23,180 KB
testcase_23 AC 171 ms
22,968 KB
testcase_24 AC 192 ms
22,900 KB
testcase_25 AC 185 ms
22,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
#include <tuple>
#include <climits>
#include <bitset>
#include <cassert>
#include <random>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int N = 5e6+1;
int np[N];

int need[10];
int cnt[10];

bool can(){
	rep(i, 10) if (!need[i] && cnt[i]) return false;
	return true;
}

bool check(){
	rep(i, 10) if (need[i] != (cnt[i] >= 1)) return false;
	return true;
}

void f(int val, int k){
	if (!val) return;
	cnt[val % 10] += k;
	f(val / 10, k);
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
np[1]=1;
	for (int i = 2; i*i < N; ++i){
		if (!np[i]) for (int j = i * 2; j < N; j += i) np[j] = 1;
	}

	int n;
	cin >> n;
	rep(i, n){
		int x;
		cin >> x;
		need[x] = 1;
	}

	int l = 1, r = 1;
	int ans = -1;
	while (1){
		while (r < N && can()){
			if(check()) ans = max(ans, r - l - 1);
			if (!np[r]) f(r, 1);
			++r;
		}
		if (check()) ans = max(ans, r - l - 1);

		if (l >= r) break;

		if (!np[l]) f(l, -1);
		++l;
		if(check()) ans = max(ans, r - l - 1);
	}

	cout << ans << endl;

	return 0;
}
0