結果

問題 No.12 限定された素数
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-09-30 00:46:07
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,564 bytes
コンパイル時間 708 ms
コンパイル使用メモリ 70,820 KB
実行使用メモリ 8,636 KB
最終ジャッジ日時 2023-08-23 10:53:41
合計ジャッジ時間 13,047 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 373 ms
8,444 KB
testcase_01 AC 418 ms
8,320 KB
testcase_02 AC 337 ms
8,380 KB
testcase_03 AC 626 ms
8,440 KB
testcase_04 AC 368 ms
8,380 KB
testcase_05 AC 411 ms
8,380 KB
testcase_06 AC 436 ms
8,440 KB
testcase_07 AC 491 ms
8,388 KB
testcase_08 AC 377 ms
8,552 KB
testcase_09 AC 395 ms
8,340 KB
testcase_10 AC 405 ms
8,380 KB
testcase_11 AC 535 ms
8,460 KB
testcase_12 AC 449 ms
8,564 KB
testcase_13 AC 436 ms
8,584 KB
testcase_14 AC 409 ms
8,388 KB
testcase_15 AC 434 ms
8,388 KB
testcase_16 AC 470 ms
8,464 KB
testcase_17 AC 440 ms
8,320 KB
testcase_18 AC 322 ms
8,508 KB
testcase_19 AC 323 ms
8,324 KB
testcase_20 AC 333 ms
8,376 KB
testcase_21 AC 385 ms
8,332 KB
testcase_22 AC 342 ms
8,384 KB
testcase_23 AC 340 ms
8,332 KB
testcase_24 AC 405 ms
8,500 KB
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

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()){//含んでいけない文字を含んでしまっているので、左を縮める
			while(!isprime[l]) l++;//素数のつぎの数まで縮める
			string tmp = to_string(l);
			rep(j, tmp.size()){
				cnt[tmp[j] - '0']--;
			}
			l++;
		}

		if(ok()){//条件を満たす
			ans = max(ans, r - l);
		}
	}
	printf("%d\n", ans);
}
0