結果

問題 No.12 限定された素数
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-09-30 01:14:20
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 633 ms / 5,000 ms
コード長 1,510 bytes
コンパイル時間 1,054 ms
コンパイル使用メモリ 70,060 KB
実行使用メモリ 8,552 KB
最終ジャッジ日時 2023-08-15 23:34:10
合計ジャッジ時間 11,276 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 305 ms
8,360 KB
testcase_01 AC 347 ms
8,356 KB
testcase_02 AC 242 ms
8,416 KB
testcase_03 AC 633 ms
8,436 KB
testcase_04 AC 274 ms
8,476 KB
testcase_05 AC 357 ms
8,360 KB
testcase_06 AC 397 ms
8,352 KB
testcase_07 AC 438 ms
8,352 KB
testcase_08 AC 310 ms
8,348 KB
testcase_09 AC 337 ms
8,364 KB
testcase_10 AC 357 ms
8,416 KB
testcase_11 AC 497 ms
8,368 KB
testcase_12 AC 390 ms
8,416 KB
testcase_13 AC 396 ms
8,356 KB
testcase_14 AC 378 ms
8,340 KB
testcase_15 AC 397 ms
8,356 KB
testcase_16 AC 406 ms
8,360 KB
testcase_17 AC 373 ms
8,404 KB
testcase_18 AC 248 ms
8,416 KB
testcase_19 AC 248 ms
8,552 KB
testcase_20 AC 276 ms
8,428 KB
testcase_21 AC 332 ms
8,416 KB
testcase_22 AC 265 ms
8,360 KB
testcase_23 AC 264 ms
8,368 KB
testcase_24 AC 346 ms
8,484 KB
testcase_25 AC 409 ms
8,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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);
}
0