結果

問題 No.563 超高速一人かるた large
ユーザー pekempeypekempey
提出日時 2017-08-26 01:05:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,461 bytes
コンパイル時間 1,384 ms
コンパイル使用メモリ 101,908 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2024-04-23 15:54:56
合計ジャッジ時間 10,576 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
5,760 KB
testcase_01 AC 7 ms
5,888 KB
testcase_02 AC 7 ms
5,760 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 951 ms
6,272 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 943 ms
6,272 KB
testcase_18 WA -
testcase_19 AC 11 ms
6,000 KB
testcase_20 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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) {
      for (int j = 1; j <= n; j++) {
        int x = c;
        int y = c + kv.second;
        ans[j] += (kv.first - 1) * P(j - 1, y) * P(n - 1 - y, j - 1 - y);
        ans[j] -= (kv.first - 1) * P(j - 1, x) * P(n - 1 - x, j - 1 - x);
      }
      c += kv.second;
      // ans[c] += -kv.first * fact[c - 1] * fact[n - c];
    }
  }

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