結果

問題 No.12 限定された素数
ユーザー IL_mstaIL_msta
提出日時 2015-07-11 10:38:51
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 62 ms / 5,000 ms
コード長 2,543 bytes
コンパイル時間 1,083 ms
コンパイル使用メモリ 89,480 KB
実行使用メモリ 10,144 KB
最終ジャッジ日時 2023-08-15 23:01:11
合計ジャッジ時間 3,612 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
9,836 KB
testcase_01 AC 60 ms
10,092 KB
testcase_02 AC 60 ms
9,840 KB
testcase_03 AC 46 ms
9,968 KB
testcase_04 AC 61 ms
10,024 KB
testcase_05 AC 61 ms
10,088 KB
testcase_06 AC 61 ms
9,824 KB
testcase_07 AC 61 ms
9,884 KB
testcase_08 AC 61 ms
10,144 KB
testcase_09 AC 61 ms
10,024 KB
testcase_10 AC 60 ms
10,028 KB
testcase_11 AC 59 ms
10,024 KB
testcase_12 AC 61 ms
9,896 KB
testcase_13 AC 62 ms
10,040 KB
testcase_14 AC 62 ms
10,092 KB
testcase_15 AC 61 ms
9,824 KB
testcase_16 AC 61 ms
9,940 KB
testcase_17 AC 59 ms
10,096 KB
testcase_18 AC 59 ms
9,964 KB
testcase_19 AC 60 ms
9,832 KB
testcase_20 AC 60 ms
10,088 KB
testcase_21 AC 60 ms
10,084 KB
testcase_22 AC 59 ms
9,836 KB
testcase_23 AC 59 ms
10,112 KB
testcase_24 AC 59 ms
9,832 KB
testcase_25 AC 59 ms
9,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES

#include <iostream>
#include <iomanip>

#include <algorithm>
#include <cmath>

#include <string>
#include <array>
#include <list>
#include <queue>
#include <vector>
#include <complex>
#include <set>
#include <map>

/////////
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define P(p) cout<<(p)<<endl;

#define PII pair<int,int>
/////////
typedef long long LL;
typedef long double LD;
/////////
using namespace::std;
/////////
const int NumMax = 5000000;
bool pdp[NumMax+1];
vector<int> prime;
//2からNまでの素数を作る-usingめぐちゃん
void makePrime(int N){
	for(int i=2; i<= N; ++i){
		if(pdp[i] == false){
			prime.push_back(i);
			for(int j = i+i; j<= N; j += i){
				pdp[j] = true;
			}
		}
	}
	return ;
}

void NumCount(int num,int* A,bool flag=false){
	int n = num;
	while(n!=0){
		if(flag == true){
			--A[(n%10)];
		}else{
			++A[(n%10)];
		}
		n /= 10;
	}
	return;
}

int check(bool* A,int* Num){
	rep(i,10){
		if(A[i] == false && Num[i] > 0){//
			return 0;//使っちゃダメ
		}
		if(A[i] == true && Num[i] == 0){
			return 1;//数字が足りない
		}
	}
	return 2;//OK
}
int main(void){
    std::cin.tie(0); 
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed;//
    cout << setprecision(10);//

	makePrime(NumMax);

	int N;
	cin>>N;
	
	string str;
	bool A[10];
	rep(i,10)A[i] = false;
	rep(i,N){
		cin>>str;
		A[str[0]-'0'] = true;
	}

	if(N==10){
		P(5000000-1);
		return 0;
	}

	vector<int>::iterator Left,Right;
	Left = prime.begin();
	Right = prime.begin();

	int NumNum[10];
	rep(i,10)NumNum[i]=0;
	NumCount(*Right,NumNum);
	int ans = -1;
	int temp;
	int checkRes;
	while( Left != prime.end() ){
		//cout << *Left << " " << *Right << endl;
		checkRes = check(A,NumNum);
		while( checkRes != 0 ){//範囲でcheck
			if( checkRes == 2){
				if(Right == prime.end()-1){
					temp = 5000000;
				}else{
					temp = *(Right+1)-1;
				}
				if(Left == prime.begin() ){
					temp = temp - 1;
				}else{
					temp = temp - *(Left-1) - 1;
				}
				
				if( ans < temp ){
					ans = temp;
					//cout << *Left << " " << *Right << " " << ans << endl;
				}
			}
			//////
			if(Right != prime.end() -1 ){
				++Right;
				NumCount(*Right,NumNum);
			}else{
				break;
			}
			checkRes = check(A,NumNum);
		}
		//checkRes ==0 || Right == prime.end()-1
		if(Left == prime.end() -1){
			break;
		}
		else if(Left == Right){
			++Right;
			NumCount(*Right,NumNum);
		}else{
			NumCount(*Left,NumNum,true);
			++Left;
		}
	}
	P(ans);
    return 0;
}
0