結果
| 問題 |
No.12 限定された素数
|
| コンテスト | |
| ユーザー |
IL_msta
|
| 提出日時 | 2015-07-11 10:42:14 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 65 ms / 5,000 ms |
| コード長 | 2,608 bytes |
| コンパイル時間 | 778 ms |
| コンパイル使用メモリ | 88,752 KB |
| 実行使用メモリ | 10,228 KB |
| 最終ジャッジ日時 | 2024-11-24 08:22:58 |
| 合計ジャッジ時間 | 3,315 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#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){
int ans = 2;
rep(i,10){
if(A[i] == false && Num[i] > 0){//
return 0;//使っちゃダメ
}
if(A[i] == true && Num[i] == 0){
ans = 1;
//return 1;//数字が足りない
}
}
//数字が足りないだけ||OK
return ans;
}
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;
}
IL_msta