結果

問題 No.12 限定された素数
ユーザー k82b
提出日時 2023-10-10 06:55:39
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 110 ms / 5,000 ms
コード長 1,573 bytes
コンパイル時間 1,758 ms
コンパイル使用メモリ 180,280 KB
実行使用メモリ 9,624 KB
最終ジャッジ日時 2024-09-13 00:14:02
合計ジャッジ時間 5,369 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

void main() {
	const N = readInt;
	const M = 5000000;
	auto ok = new bool[10];
	foreach (i; 0 .. N) {
		const a = readInt;
		ok[a] = true;
	}
	int[] A;
	A ~= 0;
	{
		auto S = new bool[M];
		foreach (i; 2 .. M) {
			if (S[i]) continue;
			A ~= i;
			for (int j = i * 2; j < M; j += i) S[j] = true;
		}
	}
	A ~= M + 1;
	int AL = cast(int)(A.length);
	while (A[AL - 1] > M) AL--;
	auto cnt = new int[10];
	int L = 1, V = N, W;
	void add(int x) {
		while (x) {
			int y = x % 10;
			if (ok[y] && ++cnt[y] == 1) V--;
			if (!ok[y] && ++cnt[y] == 1) W++;
			x /= 10;
		}
	}
	void del(int x) {
		while (x) {
			int y = x % 10;
			if (ok[y] && !--cnt[y]) V++;
			if (!ok[y] && !--cnt[y]) W--;
			x /= 10;
		}
	}
	int ans = -1;
	foreach (R; 1 .. AL) {
		add(A[R]);
		while (L <= R && W) del(A[L++]);
		if (L > R || V) continue;
		chmax(ans, A[R + 1] - A[L - 1] - 2);
	}
	ans.writeln;
}

import std,core.bitop;
string[]_R;
string readString(){while(_R.empty){_R=readln.chomp.split;}auto ret=_R.front;_R.popFront;return ret;}
int readInt(){return readString.to!int;}
long readLong(){return readString.to!long;}
ulong readULong(){return readString.to!ulong;}
real readReal(){return readString.to!real;}
bool chmin(T)(ref T A,T B){if(A>B){A=B;return true;}else{return false;}}
bool chmax(T)(ref T A,T B){if(A<B){A=B;return true;}else{return false;}}
int lowerBound(T)(T[]A,T x){int L=-1,R=cast(int)A.length;while(R-L>1){int mid=(L+R)/2;(A[mid]<x?L:R)=mid;}return R;}
int upperBound(T)(T[]A,T x){int L=-1,R=cast(int)A.length;while(R-L>1){int mid=(L+R)/2;(A[mid]<=x?L:R)=mid;}return R;}
0