結果
問題 | No.12 限定された素数 |
ユーザー |
|
提出日時 | 2017-01-13 18:10:13 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 59 ms / 5,000 ms |
コード長 | 1,637 bytes |
コンパイル時間 | 846 ms |
コンパイル使用メモリ | 132,036 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-06-12 06:25:07 |
合計ジャッジ時間 | 3,144 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.container; // SList, DList, BinaryHeap import std.typecons; // Tuple, Nullable, BigFlags import std.math; // math functions import std.numeric; // gcd import std.bigint; // BigInt import std.random; // random import std.bitmanip; // BitArray import core.bitop; // bit operation import std.regex; // RegEx import std.uni; // unicode void main() { auto n = readln.chomp.to!size_t; auto ai = readln.split.to!(int[]); writeln(calc(ai)); } int calc(int[] ai) { auto ma = 5_000_000; auto pi = primes(ma); auto s = ai.map!toBitNum.fold!"a | b"; auto t = ~s & ((1 << 10) - 1); auto r = -1, i = 0; while (i < pi.length) { while (toBitNum(pi[i]) & t) { if (++i >= pi.length) return r; } auto j = i; while (!(toBitNum(pi[j]) & t)) { if (j++ >= pi.length - 1) break; } if (pi[i..j].map!toBitNum.fold!"a | b" == s) { auto p1 = i == 0 ? 1 : pi[i - 1] + 1; auto p2 = j == pi.length ? ma : pi[j] - 1; r = max(r, p2 - p1); } i = j + 1; } return r; } int toBitNum(int a) { if (a == 0) return 1; int r = 0; for (; a != 0; a /= 10) r |= (1 << a % 10); return r; } int[] primes(int n) { auto sieve = new bool[]((n + 1) / 2); sieve[] = true; foreach (p; iota(1, (n.to!real.sqrt.to!int - 1) / 2 + 1)) if (sieve[p]) foreach (q; iota(p * 3 + 1, (n + 1) / 2, p * 2 + 1)) sieve[q] = false; auto r = sieve.enumerate .filter!("a[1]") .map!("a[0]").map!("a * 2 + 1") .map!(to!int).array; r[0] = 2; return r; }