#include <bits/stdc++.h>
using i64 = long long;
constexpr int L = 8;
constexpr i64 mod = 998244353;
/*
value of C
subtask 1 : 6
subtask 2 : 16
subtask 3 : 32
subtask 4 : 62
*/
int main() {
int N;
std::cin >> N;
std::vector<std::string> words(N);
for (auto &e : words) {
std::cin >> e;
auto b = e;
std::reverse(b.begin(), b.end());
if (e > b) {
e = b;
}
}
std::sort(words.begin(), words.end());
words.erase(std::unique(words.begin(), words.end()), words.end());
N = (int)words.size();
std::vector<std::vector<int>> W(N);
std::vector<char> chars;
for (const auto &e : words) {
for (const auto c : e) {
chars.push_back(c);
}
}
std::sort(chars.begin(), chars.end());
chars.erase(std::unique(chars.begin(), chars.end()), chars.end());
for (int i = 0; i < N; ++i) {
for (const auto e : words[i]) {
W[i].push_back((int)(std::lower_bound(chars.begin(), chars.end(), e) - chars.begin()));
}
}
const int C = chars.size();
std::array<std::vector<std::vector<i64>>, L> U;
for (auto &vec : U) {
vec.resize(C);
for (auto &vec2 : vec) {
vec2.assign(C, 0);
}
}
for (int i = 0; i < N; ++i) {
const int a = (int)W[i].size();
++U[a - 3][W[i][0]][W[i][a - 1]];
auto w2 = W[i];
std::reverse(w2.begin(), w2.end());
if (W[i] != w2) {
++U[a - 3][w2[0]][w2[a - 1]];
}
}
auto solve = [&](std::vector<std::vector<i64>> V) -> i64 {
// C^8
std::map<std::array<int, 4>, i64> memo;
auto calc = [&](std::array<int, 4> cList) -> i64 {
i64 ret = 1;
for (int x = 0; x < 4; ++x) {
i64 s = 0;
for (int i = 0; i < C; ++i) {
i64 c = 1;
for (int j = 0; j < 4; ++j) {
if (x != j) c *= V[cList[j]][i];
}
s += c;
s %= mod;
}
ret *= s;
ret %= mod;
}
return ret;
};
i64 res = 0;
for (int a = 0; a < C; ++a) {
for (int b = 0; b < C; ++b) {
for (int c = 0; c < C; ++c) {
for (int d = 0; d < C; ++d) {
std::array<int, 4> list = {{a, b, c, d}};
std::sort(list.begin(), list.end());
if (memo.find(list) != memo.end()) {
res += memo[list];
} else {
res += calc(list);
}
res %= mod;
}
}
}
}
return res;
};
i64 answer = 0;
for (const auto &e : U) {
answer += solve(e);
answer %= mod;
}
std::cout << answer << std::endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
11832 KB |
Output is correct |
2 |
Correct |
108 ms |
12728 KB |
Output is correct |
3 |
Correct |
105 ms |
12724 KB |
Output is correct |
4 |
Correct |
108 ms |
12736 KB |
Output is correct |
5 |
Correct |
107 ms |
12724 KB |
Output is correct |
6 |
Correct |
104 ms |
12720 KB |
Output is correct |
7 |
Correct |
108 ms |
12864 KB |
Output is correct |
8 |
Correct |
104 ms |
12732 KB |
Output is correct |
9 |
Correct |
104 ms |
12720 KB |
Output is correct |
10 |
Correct |
107 ms |
12724 KB |
Output is correct |
11 |
Correct |
108 ms |
12724 KB |
Output is correct |
12 |
Correct |
103 ms |
12732 KB |
Output is correct |
13 |
Correct |
107 ms |
12732 KB |
Output is correct |
14 |
Correct |
104 ms |
12736 KB |
Output is correct |
15 |
Correct |
104 ms |
12740 KB |
Output is correct |
16 |
Correct |
109 ms |
12784 KB |
Output is correct |
17 |
Correct |
106 ms |
12720 KB |
Output is correct |
18 |
Correct |
106 ms |
12704 KB |
Output is correct |
19 |
Correct |
106 ms |
12628 KB |
Output is correct |
20 |
Correct |
104 ms |
12720 KB |
Output is correct |
21 |
Correct |
106 ms |
12736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
11832 KB |
Output is correct |
2 |
Correct |
108 ms |
12728 KB |
Output is correct |
3 |
Correct |
105 ms |
12724 KB |
Output is correct |
4 |
Correct |
108 ms |
12736 KB |
Output is correct |
5 |
Correct |
107 ms |
12724 KB |
Output is correct |
6 |
Correct |
104 ms |
12720 KB |
Output is correct |
7 |
Correct |
108 ms |
12864 KB |
Output is correct |
8 |
Correct |
104 ms |
12732 KB |
Output is correct |
9 |
Correct |
104 ms |
12720 KB |
Output is correct |
10 |
Correct |
107 ms |
12724 KB |
Output is correct |
11 |
Correct |
108 ms |
12724 KB |
Output is correct |
12 |
Correct |
103 ms |
12732 KB |
Output is correct |
13 |
Correct |
107 ms |
12732 KB |
Output is correct |
14 |
Correct |
104 ms |
12736 KB |
Output is correct |
15 |
Correct |
104 ms |
12740 KB |
Output is correct |
16 |
Correct |
109 ms |
12784 KB |
Output is correct |
17 |
Correct |
106 ms |
12720 KB |
Output is correct |
18 |
Correct |
106 ms |
12704 KB |
Output is correct |
19 |
Correct |
106 ms |
12628 KB |
Output is correct |
20 |
Correct |
104 ms |
12720 KB |
Output is correct |
21 |
Correct |
106 ms |
12736 KB |
Output is correct |
22 |
Correct |
284 ms |
12384 KB |
Output is correct |
23 |
Correct |
279 ms |
12260 KB |
Output is correct |
24 |
Correct |
280 ms |
12384 KB |
Output is correct |
25 |
Correct |
280 ms |
12384 KB |
Output is correct |
26 |
Correct |
287 ms |
12480 KB |
Output is correct |
27 |
Correct |
280 ms |
12332 KB |
Output is correct |
28 |
Correct |
280 ms |
12352 KB |
Output is correct |
29 |
Correct |
284 ms |
12380 KB |
Output is correct |
30 |
Correct |
283 ms |
12584 KB |
Output is correct |
31 |
Correct |
298 ms |
12372 KB |
Output is correct |
32 |
Correct |
288 ms |
12352 KB |
Output is correct |
33 |
Correct |
283 ms |
12376 KB |
Output is correct |
34 |
Correct |
285 ms |
12632 KB |
Output is correct |
35 |
Correct |
280 ms |
12380 KB |
Output is correct |
36 |
Correct |
293 ms |
12480 KB |
Output is correct |
37 |
Correct |
281 ms |
12500 KB |
Output is correct |
38 |
Correct |
281 ms |
12380 KB |
Output is correct |
39 |
Correct |
284 ms |
12388 KB |
Output is correct |
40 |
Correct |
288 ms |
12500 KB |
Output is correct |
41 |
Correct |
282 ms |
12500 KB |
Output is correct |
42 |
Correct |
287 ms |
12352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
11832 KB |
Output is correct |
2 |
Correct |
108 ms |
12728 KB |
Output is correct |
3 |
Correct |
105 ms |
12724 KB |
Output is correct |
4 |
Correct |
108 ms |
12736 KB |
Output is correct |
5 |
Correct |
107 ms |
12724 KB |
Output is correct |
6 |
Correct |
104 ms |
12720 KB |
Output is correct |
7 |
Correct |
108 ms |
12864 KB |
Output is correct |
8 |
Correct |
104 ms |
12732 KB |
Output is correct |
9 |
Correct |
104 ms |
12720 KB |
Output is correct |
10 |
Correct |
107 ms |
12724 KB |
Output is correct |
11 |
Correct |
108 ms |
12724 KB |
Output is correct |
12 |
Correct |
103 ms |
12732 KB |
Output is correct |
13 |
Correct |
107 ms |
12732 KB |
Output is correct |
14 |
Correct |
104 ms |
12736 KB |
Output is correct |
15 |
Correct |
104 ms |
12740 KB |
Output is correct |
16 |
Correct |
109 ms |
12784 KB |
Output is correct |
17 |
Correct |
106 ms |
12720 KB |
Output is correct |
18 |
Correct |
106 ms |
12704 KB |
Output is correct |
19 |
Correct |
106 ms |
12628 KB |
Output is correct |
20 |
Correct |
104 ms |
12720 KB |
Output is correct |
21 |
Correct |
106 ms |
12736 KB |
Output is correct |
22 |
Correct |
284 ms |
12384 KB |
Output is correct |
23 |
Correct |
279 ms |
12260 KB |
Output is correct |
24 |
Correct |
280 ms |
12384 KB |
Output is correct |
25 |
Correct |
280 ms |
12384 KB |
Output is correct |
26 |
Correct |
287 ms |
12480 KB |
Output is correct |
27 |
Correct |
280 ms |
12332 KB |
Output is correct |
28 |
Correct |
280 ms |
12352 KB |
Output is correct |
29 |
Correct |
284 ms |
12380 KB |
Output is correct |
30 |
Correct |
283 ms |
12584 KB |
Output is correct |
31 |
Correct |
298 ms |
12372 KB |
Output is correct |
32 |
Correct |
288 ms |
12352 KB |
Output is correct |
33 |
Correct |
283 ms |
12376 KB |
Output is correct |
34 |
Correct |
285 ms |
12632 KB |
Output is correct |
35 |
Correct |
280 ms |
12380 KB |
Output is correct |
36 |
Correct |
293 ms |
12480 KB |
Output is correct |
37 |
Correct |
281 ms |
12500 KB |
Output is correct |
38 |
Correct |
281 ms |
12380 KB |
Output is correct |
39 |
Correct |
284 ms |
12388 KB |
Output is correct |
40 |
Correct |
288 ms |
12500 KB |
Output is correct |
41 |
Correct |
282 ms |
12500 KB |
Output is correct |
42 |
Correct |
287 ms |
12352 KB |
Output is correct |
43 |
Execution timed out |
1178 ms |
12220 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
11832 KB |
Output is correct |
2 |
Correct |
108 ms |
12728 KB |
Output is correct |
3 |
Correct |
105 ms |
12724 KB |
Output is correct |
4 |
Correct |
108 ms |
12736 KB |
Output is correct |
5 |
Correct |
107 ms |
12724 KB |
Output is correct |
6 |
Correct |
104 ms |
12720 KB |
Output is correct |
7 |
Correct |
108 ms |
12864 KB |
Output is correct |
8 |
Correct |
104 ms |
12732 KB |
Output is correct |
9 |
Correct |
104 ms |
12720 KB |
Output is correct |
10 |
Correct |
107 ms |
12724 KB |
Output is correct |
11 |
Correct |
108 ms |
12724 KB |
Output is correct |
12 |
Correct |
103 ms |
12732 KB |
Output is correct |
13 |
Correct |
107 ms |
12732 KB |
Output is correct |
14 |
Correct |
104 ms |
12736 KB |
Output is correct |
15 |
Correct |
104 ms |
12740 KB |
Output is correct |
16 |
Correct |
109 ms |
12784 KB |
Output is correct |
17 |
Correct |
106 ms |
12720 KB |
Output is correct |
18 |
Correct |
106 ms |
12704 KB |
Output is correct |
19 |
Correct |
106 ms |
12628 KB |
Output is correct |
20 |
Correct |
104 ms |
12720 KB |
Output is correct |
21 |
Correct |
106 ms |
12736 KB |
Output is correct |
22 |
Correct |
284 ms |
12384 KB |
Output is correct |
23 |
Correct |
279 ms |
12260 KB |
Output is correct |
24 |
Correct |
280 ms |
12384 KB |
Output is correct |
25 |
Correct |
280 ms |
12384 KB |
Output is correct |
26 |
Correct |
287 ms |
12480 KB |
Output is correct |
27 |
Correct |
280 ms |
12332 KB |
Output is correct |
28 |
Correct |
280 ms |
12352 KB |
Output is correct |
29 |
Correct |
284 ms |
12380 KB |
Output is correct |
30 |
Correct |
283 ms |
12584 KB |
Output is correct |
31 |
Correct |
298 ms |
12372 KB |
Output is correct |
32 |
Correct |
288 ms |
12352 KB |
Output is correct |
33 |
Correct |
283 ms |
12376 KB |
Output is correct |
34 |
Correct |
285 ms |
12632 KB |
Output is correct |
35 |
Correct |
280 ms |
12380 KB |
Output is correct |
36 |
Correct |
293 ms |
12480 KB |
Output is correct |
37 |
Correct |
281 ms |
12500 KB |
Output is correct |
38 |
Correct |
281 ms |
12380 KB |
Output is correct |
39 |
Correct |
284 ms |
12388 KB |
Output is correct |
40 |
Correct |
288 ms |
12500 KB |
Output is correct |
41 |
Correct |
282 ms |
12500 KB |
Output is correct |
42 |
Correct |
287 ms |
12352 KB |
Output is correct |
43 |
Execution timed out |
1178 ms |
12220 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |