結果

問題 No.12 限定された素数
ユーザー kyuridenamidakyuridenamida
提出日時 2016-02-10 20:31:02
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 136 ms / 5,000 ms
コード長 1,463 bytes
コンパイル時間 1,494 ms
コンパイル使用メモリ 151,052 KB
実行使用メモリ 24,940 KB
最終ジャッジ日時 2023-08-15 23:18:40
合計ジャッジ時間 6,066 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
24,804 KB
testcase_01 AC 95 ms
24,592 KB
testcase_02 AC 92 ms
24,804 KB
testcase_03 AC 136 ms
24,664 KB
testcase_04 AC 91 ms
24,596 KB
testcase_05 AC 95 ms
24,940 KB
testcase_06 AC 100 ms
24,668 KB
testcase_07 AC 111 ms
24,596 KB
testcase_08 AC 92 ms
24,796 KB
testcase_09 AC 87 ms
24,744 KB
testcase_10 AC 87 ms
24,592 KB
testcase_11 AC 133 ms
24,656 KB
testcase_12 AC 106 ms
24,792 KB
testcase_13 AC 95 ms
24,736 KB
testcase_14 AC 87 ms
24,796 KB
testcase_15 AC 102 ms
24,728 KB
testcase_16 AC 118 ms
24,660 KB
testcase_17 AC 87 ms
24,740 KB
testcase_18 AC 90 ms
24,664 KB
testcase_19 AC 87 ms
24,600 KB
testcase_20 AC 89 ms
24,668 KB
testcase_21 AC 96 ms
24,652 KB
testcase_22 AC 88 ms
24,656 KB
testcase_23 AC 88 ms
24,788 KB
testcase_24 AC 85 ms
24,728 KB
testcase_25 AC 93 ms
24,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int p[5000010];
vector<int> t(10);
void f(int n){
	if( n == 0 ){
		t = vector<int>(10);
		return;
	}
	f(n/10);
	t[n%10]++;
	return;
}

int bit = 0;
bool noChance(vector<int> c){
	for(int i = 0 ; i < 10 ; i++){
		if( !(bit>>i&1) && c[i] ){
			return true;
		}
	}
	return false;
}
bool ok(vector<int> c){
	for(int i = 0 ; i < 10 ; i++){
		if( (bit>>i&1) ^ (!!c[i]) ){
			return false;
		}
	}
	return true;
}
int main(){
	for(int i = 2 ; i <= 5000000 ; i++)
		p[i] = 1;
	int N;
	cin >> N;
	
	for(int i = 0 ; i < N ; i++) {
		int x;
		cin >> x;
		bit |= (1<<x);
	}
	
	
	vector<int> now(10);
	vector<int> P,Q;
	for(long long i = 2 ; i <= 5000000 ; i++){	
		if( p[i] ){
			P.push_back(i);
			for(long long j = i*i ; j <= 5000000 ; j += i )
				p[j] = 0;
		}
	}	
	int j = 0;
	int ans = -1;
	for(int i = 0 ; i < P.size() ; i++){
		while(j < P.size()){
			f(P[j]);
			vector<int> tnow = now;
			for(int j = 0 ; j < 10 ; j++)
				tnow[j] += t[j];
			if( noChance(tnow) ) break;
			now = tnow;
			j++;
		}
		if( i == j ){	
			j++;
		}else{
			if(ok(now)){
				int A = P[i];
				int B = P[j-1];
				while( B < 5000000 && !p[B+1] ) B++;
				while( A > 1 && !p[A-1] ) A--;
				// if( B - A == 158 || B - A == 166 ) 
					// cout << "(" << B-A << ")" << A << " " << B << "|" << P[i] << " " << P[j] << endl;
				ans = max(ans,B-A);
			}
			f(P[i]);
			for(int j = 0 ; j < 10 ; j++)
				now[j] -= t[j];
		}
	}
	cout << ans << endl;
}
0