結果
| 問題 |
No.563 超高速一人かるた large
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-01-29 23:36:28 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 126 ms / 3,000 ms |
| コード長 | 3,756 bytes |
| コンパイル時間 | 1,058 ms |
| コンパイル使用メモリ | 125,312 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-13 03:50:48 |
| 合計ジャッジ時間 | 3,574 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 18 |
ソースコード
import std.conv, std.functional, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
enum MO = 1_000_000_007L;
enum LIM = 2000 + 10;
long[] inv, fac, invFac;
void prepare() {
inv = new long[LIM];
fac = new long[LIM];
invFac = new long[LIM];
inv[1] = 1;
foreach (i; 2 .. LIM) {
inv[i] = MO - ((MO / i) * inv[cast(size_t)(MO % i)]) % MO;
}
fac[0] = invFac[0] = 1;
foreach (i; 1 .. LIM) {
fac[i] = (fac[i - 1] * i) % MO;
invFac[i] = (invFac[i - 1] * inv[i]) % MO;
}
}
int N;
string[] S;
void main() {
prepare();
try {
for (; ; ) {
N = readInt();
S = new string[N];
foreach (i; 0 .. N) {
S[i] = readToken() ~ "$";
}
S.sort;
auto L = new int[N];
foreach (i; 0 .. N) {
L[i] = cast(int)(S[i].length);
}
auto lcp = new int[N - 1];
foreach (i; 0 .. N - 1) {
assert(S[i] != S[i + 1]);
foreach (pos; 0 .. min(L[i], L[i + 1])) {
if (S[i][pos] != S[i + 1][pos]) {
lcp[i] = pos;
break;
}
}
}
debug {
writeln("S = ", S);
writeln("lcp = ", lcp);
}
auto cnt = new long[N];
foreach (i; 0 .. N) {
for (int l = L[i], a = i, b = i; l > 0; --l) {
for (; a > 0 && lcp[a - 1] >= l - 1; --a) {}
for (; b < N - 1 && lcp[b] >= l - 1; ++b) {}
debug {
if (N <= 3) {
writefln("i = %s, l = %s, [a, b] = [%s, %s]", i, l, a, b);
}
}
++cnt[b - a];
}
}
debug {
writeln("cnt = ", cnt);
}
auto f = new long[N];
foreach (j; 0 .. N) {
foreach (x; 0 .. N) {
// P(N - 1, j) - P(j, x) * P(N - 1 - x, j - x)
long tmp;
tmp += (fac[N - 1] * invFac[(N - 1) - j]) % MO;
if (j >= x) {
tmp -= (((fac[j] * invFac[j - x]) % MO) * ((fac[N - 1 - x] * invFac[(N - 1 - x) - (j - x)]) % MO)) % MO;
}
tmp = (tmp % MO + MO) % MO;
(tmp *= cnt[x]) %= MO;
debug {
// writeln(j, " ", x, ": ", tmp);
}
(f[j] += tmp) %= MO;
}
}
debug {
writeln("f = ", f);
}
auto ans = new long[N + 1];
foreach (k; 1 .. N + 1) {
foreach (j; 0 .. k) {
// f[j] * P(N - 1 - j, k - 1 - j)
long tmp = f[j];
tmp *= ((fac[N - 1 - j] * invFac[(N - 1 - j) - (k - 1 - j)]) % MO);
tmp %= MO;
(ans[k] += tmp) %= MO;
}
}
(ans[N] += fac[N]) %= MO;
foreach (k; 1 .. N + 1) {
writeln(ans[k]);
}
}
} catch (EOFException e) {
}
}