結果
問題 | No.563 超高速一人かるた large |
ユーザー | pekempey |
提出日時 | 2017-08-26 01:22:23 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 828 ms / 3,000 ms |
コード長 | 2,548 bytes |
コンパイル時間 | 1,371 ms |
コンパイル使用メモリ | 102,348 KB |
実行使用メモリ | 6,144 KB |
最終ジャッジ日時 | 2024-10-15 16:23:00 |
合計ジャッジ時間 | 6,035 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 8 ms
5,760 KB |
testcase_01 | AC | 8 ms
5,760 KB |
testcase_02 | AC | 7 ms
5,632 KB |
testcase_03 | AC | 9 ms
5,888 KB |
testcase_04 | AC | 14 ms
5,888 KB |
testcase_05 | AC | 18 ms
5,888 KB |
testcase_06 | AC | 129 ms
6,016 KB |
testcase_07 | AC | 193 ms
6,016 KB |
testcase_08 | AC | 428 ms
6,144 KB |
testcase_09 | AC | 828 ms
6,144 KB |
testcase_10 | AC | 174 ms
5,888 KB |
testcase_11 | AC | 166 ms
5,888 KB |
testcase_12 | AC | 174 ms
5,888 KB |
testcase_13 | AC | 288 ms
5,888 KB |
testcase_14 | AC | 230 ms
5,888 KB |
testcase_15 | AC | 108 ms
5,888 KB |
testcase_16 | AC | 337 ms
5,888 KB |
testcase_17 | AC | 372 ms
6,016 KB |
testcase_18 | AC | 168 ms
6,016 KB |
testcase_19 | AC | 11 ms
6,008 KB |
testcase_20 | AC | 282 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++) { | ~~^~~~
ソースコード
#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; } }