結果

問題 No.563 超高速一人かるた large
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-29 23:36:28
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 137 ms / 3,000 ms
コード長 3,756 bytes
コンパイル時間 1,818 ms
コンパイル使用メモリ 111,564 KB
実行使用メモリ 5,188 KB
最終ジャッジ日時 2023-09-03 23:16:38
合計ジャッジ時間 3,631 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 26 ms
4,384 KB
testcase_07 AC 37 ms
4,376 KB
testcase_08 AC 73 ms
4,376 KB
testcase_09 AC 134 ms
5,188 KB
testcase_10 AC 119 ms
4,380 KB
testcase_11 AC 134 ms
4,380 KB
testcase_12 AC 133 ms
4,376 KB
testcase_13 AC 135 ms
4,380 KB
testcase_14 AC 133 ms
4,376 KB
testcase_15 AC 133 ms
4,376 KB
testcase_16 AC 134 ms
4,380 KB
testcase_17 AC 19 ms
4,376 KB
testcase_18 AC 137 ms
4,380 KB
testcase_19 AC 4 ms
4,376 KB
testcase_20 AC 78 ms
5,052 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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) {
  }
}
0