結果
| 問題 | No.12 限定された素数 | 
| コンテスト | |
| ユーザー |  srup٩(๑`н´๑)۶ | 
| 提出日時 | 2016-09-30 01:14:20 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 582 ms / 5,000 ms | 
| コード長 | 1,510 bytes | 
| コンパイル時間 | 643 ms | 
| コンパイル使用メモリ | 69,204 KB | 
| 実行使用メモリ | 8,492 KB | 
| 最終ジャッジ日時 | 2024-11-24 08:58:40 | 
| 合計ジャッジ時間 | 9,774 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 26 | 
ソースコード
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstdio>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);i++)
const int INF = 1e9;
bool isprime[5000010];
//エラトテネス
void eratos(int m){
    for (int i = 0; i <= m; ++i) isprime[i] = true;
    isprime[0] = isprime[1] = false;
    //iを残してiの倍数を消していく
    for (int i = 2; i <= m; ++i){
        if(isprime[i]){
            for (int j = 2 * i; j <= m; j += i){
                isprime[j] = false;
            }
        }
    }
}
int n;
set<int> a;
int cnt[10];
bool hantei(){
	rep(i, 10){
		if(a.count(i) == 0){//含んでいけない数
			if(cnt[i] != 0) return false;
		}
	}
	return true;
}
bool ok(){
	rep(i, 10){
		if(a.count(i) != 0){
			if(cnt[i] == 0) return false;
		}
	}
	return true;
}
int main(void){
	cin >> n;
	rep(i, n){
		int t; cin >> t;
		a.insert(t);
	}
	eratos(5000000);
	rep(i, 10) cnt[i] = 0;
	int ans = -1;
	//[l, r]でしゃくとり法
	int l = 1;
	for (int r = 1; r <= 5000000; ++r){//右を伸ばせるだけのばす
		if(isprime[r]){
			string tm = to_string(r);
			rep(i, tm.size()){
				cnt[tm[i] - '0']++;
			}
		}
		if(!hantei()){//含んでいけない文字を含んでしまっている
			//素数rで値を条件を満たさなくなったので、lをr+1まで動かす
			rep(i, 10) cnt[i] = 0;
			l = r + 1;
		}
		if(ok()){//条件を満たす
			ans = max(ans, r - l);
		}
	}
	printf("%d\n", ans);
}
            
            
            
        