結果

問題 No.563 超高速一人かるた large
ユーザー pekempeypekempey
提出日時 2017-08-26 01:22:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 782 ms / 3,000 ms
コード長 2,548 bytes
コンパイル時間 1,373 ms
コンパイル使用メモリ 102,200 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2024-04-23 15:55:51
合計ジャッジ時間 5,961 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
5,888 KB
testcase_01 AC 8 ms
5,888 KB
testcase_02 AC 8 ms
5,888 KB
testcase_03 AC 8 ms
6,144 KB
testcase_04 AC 12 ms
5,888 KB
testcase_05 AC 18 ms
6,016 KB
testcase_06 AC 123 ms
6,144 KB
testcase_07 AC 186 ms
6,272 KB
testcase_08 AC 415 ms
6,144 KB
testcase_09 AC 782 ms
6,400 KB
testcase_10 AC 170 ms
6,144 KB
testcase_11 AC 165 ms
5,888 KB
testcase_12 AC 170 ms
6,016 KB
testcase_13 AC 274 ms
5,888 KB
testcase_14 AC 216 ms
5,888 KB
testcase_15 AC 99 ms
6,144 KB
testcase_16 AC 330 ms
6,144 KB
testcase_17 AC 357 ms
6,272 KB
testcase_18 AC 162 ms
6,400 KB
testcase_19 AC 11 ms
6,144 KB
testcase_20 AC 267 ms
6,016 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:42:29: warning: iteration 200009 invokes undefined behavior [-Waggressive-loop-optimizations]
   42 |     fact[i] = i * fact[i - 1];
      |                             ^
main.cpp:41:21: note: within this loop
   41 |   for (int i = 1; i <= N; i++) {
      |                   ~~^~~~
main.cpp:46:43: warning: iteration 200008 invokes undefined behavior [-Waggressive-loop-optimizations]
   46 |     inv[i] = inv[mod % i] * (mod - mod / i);
      |                                           ^
main.cpp:45:21: note: within this loop
   45 |   for (int i = 2; i <= N; i++) {
      |                   ~~^~~~
cc1plus: warning: iteration 200009 invokes undefined behavior [-Waggressive-loop-optimizations]
main.cpp:49:21: note: within this loop
   49 |   for (int i = 1; i <= N; i++) {
      |                   ~~^~~~

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>

using namespace std;

constexpr int mod = 1e9 + 7;

struct modint {
  int n;
  modint(int n = 0) : n(n) {}
};

modint operator+(modint a, modint b) { return modint((a.n += b.n) >= mod ? a.n - mod : a.n); }
modint operator-(modint a, modint b) { return modint((a.n -= b.n) < 0 ? a.n + mod : a.n); }
modint operator*(modint a, modint b) { return modint(1LL * a.n * b.n % mod); }
modint &operator+=(modint &a, modint b) { return a = a + b; }
modint &operator-=(modint &a, modint b) { return a = a - b; }
modint &operator*=(modint &a, modint b) { return a = a * b; }

constexpr int N = 2e5 + 10;

modint fact[N];
modint ifact[N];
modint inv[N];

modint P(int n, int r) {
  if (n < 0 || r < 0 || n < r) return 0;
  return fact[n] * ifact[n - r];
}

modint C(int n, int r) {
  if (n < 0 || r < 0 || n < r) return 0;
  return fact[n] * ifact[r] * ifact[n - r];
}

int main() {
  fact[0] = 1;
  for (int i = 1; i <= N; i++) {
    fact[i] = i * fact[i - 1];
  }
  inv[1] = 1;
  for (int i = 2; i <= N; i++) {
    inv[i] = inv[mod % i] * (mod - mod / i);
  }
  ifact[0] = 1;
  for (int i = 1; i <= N; i++) {
    ifact[i] = inv[i] * ifact[i - 1];
  }

  int n;
  cin >> n;
  vector<string> s(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }

  sort(s.begin(), s.end());

  vector<int> h(n - 1);
  for (int i = 0; i + 1 < n; i++) {
    int k = 0;
    while (k < s[i].size() && k < s[i + 1].size() && s[i][k] == s[i + 1][k]) k++;
    h[i] = k;
  }

  vector<modint> ans(n + 2);

  for (int i = 0; i < n; i++) {
    vector<int> lcp(n);
    int k = s[i].size();

    for (int j = i + 1; j < n; j++) {
      k = min(k, h[j - 1]);
      lcp[j] = k;
    }
    k = s[i].size();
    for (int j = i - 1; j >= 0; j--) {
      k = min(k, h[j]);
      lcp[j] = k;
    }

    map<int, int> mp;
    for (int j = 0; j < n; j++) {
      if (i != j) mp[-lcp[j]]++;
    }

    int c = 0;
    for (auto kv : mp) {
      int x = c;
      int y = c + kv.second;
      for (int j = y + 1; j <= n; j++) {
        // fact[j-1]*ifact[j-1-y] *fact[n-1-y]*ifact[n-j]
        ans[j] -= -(kv.first - 1) * ifact[j - 1 - y] * fact[n - 1 - y];
      }
      for (int j = x + 1; j <= n; j++) {
        ans[j] += -(kv.first - 1) * ifact[j - 1 - x] * fact[n - 1 - x];
      }
      c += kv.second;
    }
  }

  for (int i = 1; i <= n; i++) {
    ans[i] *= fact[i - 1] * ifact[n - i];
    ans[i] += ans[i - 1] * (n - i + 1);
    if (i == n) ans[i] += fact[n];
    cout << ans[i].n << endl;
  }
}
0