#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#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]];
}
}
std::vector mul(C, std::vector(C, std::vector(C, 0ll)));
auto solve = [&](const std::vector<std::vector<i64>> &V) -> i64 {
for (int a = 0; a < C; ++a) {
for (int b = 0; b < C; ++b) {
for (int c = 0; c < C; ++c) {
if (not(a <= b and b <= c)) {
int na = a, nb = b, nc = c;
if (na > nb) std::swap(na, nb);
if (na > nc) std::swap(na, nc);
if (nb > nc) std::swap(nb, nc);
mul[a][b][c] = mul[na][nb][nc];
continue;
}
i64 s = 0;
for (int i = 0; i < C; ++i) {
i64 p = V[a][i] * V[b][i] * V[c][i];
s += p;
}
s %= mod;
mul[a][b][c] = s;
}
}
}
auto calc = [&](int a, int b, int c, int d) -> i64 {
i64 ret = mul[a][b][c];
ret *= mul[a][b][d];
ret %= mod;
ret *= mul[a][c][d];
ret %= mod;
ret *= mul[b][c][d];
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) {
res += calc(a, b, c, d);
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 |
113 ms |
11804 KB |
Output is correct |
2 |
Correct |
125 ms |
11760 KB |
Output is correct |
3 |
Correct |
113 ms |
11968 KB |
Output is correct |
4 |
Correct |
112 ms |
11864 KB |
Output is correct |
5 |
Correct |
107 ms |
11872 KB |
Output is correct |
6 |
Correct |
109 ms |
11840 KB |
Output is correct |
7 |
Correct |
128 ms |
11852 KB |
Output is correct |
8 |
Correct |
126 ms |
11872 KB |
Output is correct |
9 |
Correct |
105 ms |
11828 KB |
Output is correct |
10 |
Correct |
105 ms |
11860 KB |
Output is correct |
11 |
Correct |
110 ms |
11864 KB |
Output is correct |
12 |
Correct |
123 ms |
11832 KB |
Output is correct |
13 |
Correct |
122 ms |
11864 KB |
Output is correct |
14 |
Correct |
107 ms |
11864 KB |
Output is correct |
15 |
Correct |
115 ms |
11768 KB |
Output is correct |
16 |
Correct |
111 ms |
11868 KB |
Output is correct |
17 |
Correct |
129 ms |
11860 KB |
Output is correct |
18 |
Correct |
113 ms |
11832 KB |
Output is correct |
19 |
Correct |
108 ms |
11852 KB |
Output is correct |
20 |
Correct |
110 ms |
11792 KB |
Output is correct |
21 |
Correct |
114 ms |
11868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
113 ms |
11804 KB |
Output is correct |
2 |
Correct |
125 ms |
11760 KB |
Output is correct |
3 |
Correct |
113 ms |
11968 KB |
Output is correct |
4 |
Correct |
112 ms |
11864 KB |
Output is correct |
5 |
Correct |
107 ms |
11872 KB |
Output is correct |
6 |
Correct |
109 ms |
11840 KB |
Output is correct |
7 |
Correct |
128 ms |
11852 KB |
Output is correct |
8 |
Correct |
126 ms |
11872 KB |
Output is correct |
9 |
Correct |
105 ms |
11828 KB |
Output is correct |
10 |
Correct |
105 ms |
11860 KB |
Output is correct |
11 |
Correct |
110 ms |
11864 KB |
Output is correct |
12 |
Correct |
123 ms |
11832 KB |
Output is correct |
13 |
Correct |
122 ms |
11864 KB |
Output is correct |
14 |
Correct |
107 ms |
11864 KB |
Output is correct |
15 |
Correct |
115 ms |
11768 KB |
Output is correct |
16 |
Correct |
111 ms |
11868 KB |
Output is correct |
17 |
Correct |
129 ms |
11860 KB |
Output is correct |
18 |
Correct |
113 ms |
11832 KB |
Output is correct |
19 |
Correct |
108 ms |
11852 KB |
Output is correct |
20 |
Correct |
110 ms |
11792 KB |
Output is correct |
21 |
Correct |
114 ms |
11868 KB |
Output is correct |
22 |
Correct |
128 ms |
11648 KB |
Output is correct |
23 |
Correct |
127 ms |
11636 KB |
Output is correct |
24 |
Correct |
116 ms |
11612 KB |
Output is correct |
25 |
Correct |
116 ms |
11548 KB |
Output is correct |
26 |
Correct |
118 ms |
11608 KB |
Output is correct |
27 |
Correct |
122 ms |
11612 KB |
Output is correct |
28 |
Correct |
120 ms |
11624 KB |
Output is correct |
29 |
Correct |
119 ms |
11612 KB |
Output is correct |
30 |
Correct |
114 ms |
11592 KB |
Output is correct |
31 |
Correct |
119 ms |
11644 KB |
Output is correct |
32 |
Correct |
124 ms |
11576 KB |
Output is correct |
33 |
Correct |
151 ms |
11732 KB |
Output is correct |
34 |
Correct |
155 ms |
11612 KB |
Output is correct |
35 |
Correct |
114 ms |
11584 KB |
Output is correct |
36 |
Correct |
117 ms |
11652 KB |
Output is correct |
37 |
Correct |
118 ms |
11612 KB |
Output is correct |
38 |
Correct |
109 ms |
11596 KB |
Output is correct |
39 |
Correct |
113 ms |
11600 KB |
Output is correct |
40 |
Correct |
112 ms |
11616 KB |
Output is correct |
41 |
Correct |
127 ms |
11620 KB |
Output is correct |
42 |
Correct |
125 ms |
11600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
113 ms |
11804 KB |
Output is correct |
2 |
Correct |
125 ms |
11760 KB |
Output is correct |
3 |
Correct |
113 ms |
11968 KB |
Output is correct |
4 |
Correct |
112 ms |
11864 KB |
Output is correct |
5 |
Correct |
107 ms |
11872 KB |
Output is correct |
6 |
Correct |
109 ms |
11840 KB |
Output is correct |
7 |
Correct |
128 ms |
11852 KB |
Output is correct |
8 |
Correct |
126 ms |
11872 KB |
Output is correct |
9 |
Correct |
105 ms |
11828 KB |
Output is correct |
10 |
Correct |
105 ms |
11860 KB |
Output is correct |
11 |
Correct |
110 ms |
11864 KB |
Output is correct |
12 |
Correct |
123 ms |
11832 KB |
Output is correct |
13 |
Correct |
122 ms |
11864 KB |
Output is correct |
14 |
Correct |
107 ms |
11864 KB |
Output is correct |
15 |
Correct |
115 ms |
11768 KB |
Output is correct |
16 |
Correct |
111 ms |
11868 KB |
Output is correct |
17 |
Correct |
129 ms |
11860 KB |
Output is correct |
18 |
Correct |
113 ms |
11832 KB |
Output is correct |
19 |
Correct |
108 ms |
11852 KB |
Output is correct |
20 |
Correct |
110 ms |
11792 KB |
Output is correct |
21 |
Correct |
114 ms |
11868 KB |
Output is correct |
22 |
Correct |
128 ms |
11648 KB |
Output is correct |
23 |
Correct |
127 ms |
11636 KB |
Output is correct |
24 |
Correct |
116 ms |
11612 KB |
Output is correct |
25 |
Correct |
116 ms |
11548 KB |
Output is correct |
26 |
Correct |
118 ms |
11608 KB |
Output is correct |
27 |
Correct |
122 ms |
11612 KB |
Output is correct |
28 |
Correct |
120 ms |
11624 KB |
Output is correct |
29 |
Correct |
119 ms |
11612 KB |
Output is correct |
30 |
Correct |
114 ms |
11592 KB |
Output is correct |
31 |
Correct |
119 ms |
11644 KB |
Output is correct |
32 |
Correct |
124 ms |
11576 KB |
Output is correct |
33 |
Correct |
151 ms |
11732 KB |
Output is correct |
34 |
Correct |
155 ms |
11612 KB |
Output is correct |
35 |
Correct |
114 ms |
11584 KB |
Output is correct |
36 |
Correct |
117 ms |
11652 KB |
Output is correct |
37 |
Correct |
118 ms |
11612 KB |
Output is correct |
38 |
Correct |
109 ms |
11596 KB |
Output is correct |
39 |
Correct |
113 ms |
11600 KB |
Output is correct |
40 |
Correct |
112 ms |
11616 KB |
Output is correct |
41 |
Correct |
127 ms |
11620 KB |
Output is correct |
42 |
Correct |
125 ms |
11600 KB |
Output is correct |
43 |
Correct |
173 ms |
11840 KB |
Output is correct |
44 |
Correct |
172 ms |
11844 KB |
Output is correct |
45 |
Correct |
188 ms |
11832 KB |
Output is correct |
46 |
Correct |
172 ms |
11740 KB |
Output is correct |
47 |
Correct |
176 ms |
11728 KB |
Output is correct |
48 |
Correct |
191 ms |
11736 KB |
Output is correct |
49 |
Correct |
174 ms |
11740 KB |
Output is correct |
50 |
Correct |
173 ms |
11748 KB |
Output is correct |
51 |
Correct |
184 ms |
11836 KB |
Output is correct |
52 |
Correct |
174 ms |
11712 KB |
Output is correct |
53 |
Correct |
172 ms |
11840 KB |
Output is correct |
54 |
Correct |
196 ms |
11740 KB |
Output is correct |
55 |
Correct |
182 ms |
11732 KB |
Output is correct |
56 |
Correct |
175 ms |
11876 KB |
Output is correct |
57 |
Correct |
176 ms |
11740 KB |
Output is correct |
58 |
Correct |
175 ms |
11740 KB |
Output is correct |
59 |
Correct |
174 ms |
11836 KB |
Output is correct |
60 |
Correct |
170 ms |
11740 KB |
Output is correct |
61 |
Correct |
179 ms |
11744 KB |
Output is correct |
62 |
Correct |
175 ms |
11820 KB |
Output is correct |
63 |
Correct |
174 ms |
11832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
113 ms |
11804 KB |
Output is correct |
2 |
Correct |
125 ms |
11760 KB |
Output is correct |
3 |
Correct |
113 ms |
11968 KB |
Output is correct |
4 |
Correct |
112 ms |
11864 KB |
Output is correct |
5 |
Correct |
107 ms |
11872 KB |
Output is correct |
6 |
Correct |
109 ms |
11840 KB |
Output is correct |
7 |
Correct |
128 ms |
11852 KB |
Output is correct |
8 |
Correct |
126 ms |
11872 KB |
Output is correct |
9 |
Correct |
105 ms |
11828 KB |
Output is correct |
10 |
Correct |
105 ms |
11860 KB |
Output is correct |
11 |
Correct |
110 ms |
11864 KB |
Output is correct |
12 |
Correct |
123 ms |
11832 KB |
Output is correct |
13 |
Correct |
122 ms |
11864 KB |
Output is correct |
14 |
Correct |
107 ms |
11864 KB |
Output is correct |
15 |
Correct |
115 ms |
11768 KB |
Output is correct |
16 |
Correct |
111 ms |
11868 KB |
Output is correct |
17 |
Correct |
129 ms |
11860 KB |
Output is correct |
18 |
Correct |
113 ms |
11832 KB |
Output is correct |
19 |
Correct |
108 ms |
11852 KB |
Output is correct |
20 |
Correct |
110 ms |
11792 KB |
Output is correct |
21 |
Correct |
114 ms |
11868 KB |
Output is correct |
22 |
Correct |
128 ms |
11648 KB |
Output is correct |
23 |
Correct |
127 ms |
11636 KB |
Output is correct |
24 |
Correct |
116 ms |
11612 KB |
Output is correct |
25 |
Correct |
116 ms |
11548 KB |
Output is correct |
26 |
Correct |
118 ms |
11608 KB |
Output is correct |
27 |
Correct |
122 ms |
11612 KB |
Output is correct |
28 |
Correct |
120 ms |
11624 KB |
Output is correct |
29 |
Correct |
119 ms |
11612 KB |
Output is correct |
30 |
Correct |
114 ms |
11592 KB |
Output is correct |
31 |
Correct |
119 ms |
11644 KB |
Output is correct |
32 |
Correct |
124 ms |
11576 KB |
Output is correct |
33 |
Correct |
151 ms |
11732 KB |
Output is correct |
34 |
Correct |
155 ms |
11612 KB |
Output is correct |
35 |
Correct |
114 ms |
11584 KB |
Output is correct |
36 |
Correct |
117 ms |
11652 KB |
Output is correct |
37 |
Correct |
118 ms |
11612 KB |
Output is correct |
38 |
Correct |
109 ms |
11596 KB |
Output is correct |
39 |
Correct |
113 ms |
11600 KB |
Output is correct |
40 |
Correct |
112 ms |
11616 KB |
Output is correct |
41 |
Correct |
127 ms |
11620 KB |
Output is correct |
42 |
Correct |
125 ms |
11600 KB |
Output is correct |
43 |
Correct |
173 ms |
11840 KB |
Output is correct |
44 |
Correct |
172 ms |
11844 KB |
Output is correct |
45 |
Correct |
188 ms |
11832 KB |
Output is correct |
46 |
Correct |
172 ms |
11740 KB |
Output is correct |
47 |
Correct |
176 ms |
11728 KB |
Output is correct |
48 |
Correct |
191 ms |
11736 KB |
Output is correct |
49 |
Correct |
174 ms |
11740 KB |
Output is correct |
50 |
Correct |
173 ms |
11748 KB |
Output is correct |
51 |
Correct |
184 ms |
11836 KB |
Output is correct |
52 |
Correct |
174 ms |
11712 KB |
Output is correct |
53 |
Correct |
172 ms |
11840 KB |
Output is correct |
54 |
Correct |
196 ms |
11740 KB |
Output is correct |
55 |
Correct |
182 ms |
11732 KB |
Output is correct |
56 |
Correct |
175 ms |
11876 KB |
Output is correct |
57 |
Correct |
176 ms |
11740 KB |
Output is correct |
58 |
Correct |
175 ms |
11740 KB |
Output is correct |
59 |
Correct |
174 ms |
11836 KB |
Output is correct |
60 |
Correct |
170 ms |
11740 KB |
Output is correct |
61 |
Correct |
179 ms |
11744 KB |
Output is correct |
62 |
Correct |
175 ms |
11820 KB |
Output is correct |
63 |
Correct |
174 ms |
11832 KB |
Output is correct |
64 |
Correct |
988 ms |
13800 KB |
Output is correct |
65 |
Correct |
991 ms |
14556 KB |
Output is correct |
66 |
Correct |
1010 ms |
14568 KB |
Output is correct |
67 |
Correct |
1010 ms |
14544 KB |
Output is correct |
68 |
Correct |
998 ms |
14536 KB |
Output is correct |
69 |
Correct |
985 ms |
14544 KB |
Output is correct |
70 |
Correct |
992 ms |
14528 KB |
Output is correct |
71 |
Correct |
986 ms |
14532 KB |
Output is correct |
72 |
Correct |
1020 ms |
14544 KB |
Output is correct |
73 |
Correct |
1006 ms |
14576 KB |
Output is correct |
74 |
Correct |
1079 ms |
14552 KB |
Output is correct |
75 |
Correct |
1080 ms |
14532 KB |
Output is correct |
76 |
Correct |
1082 ms |
14544 KB |
Output is correct |
77 |
Correct |
1096 ms |
14536 KB |
Output is correct |
78 |
Correct |
1068 ms |
14544 KB |
Output is correct |
79 |
Correct |
1014 ms |
14536 KB |
Output is correct |
80 |
Correct |
1051 ms |
14536 KB |
Output is correct |
81 |
Correct |
1097 ms |
14548 KB |
Output is correct |
82 |
Correct |
1047 ms |
14544 KB |
Output is correct |
83 |
Correct |
988 ms |
14548 KB |
Output is correct |
84 |
Correct |
1004 ms |
14544 KB |
Output is correct |