#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 memo[cList] = 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 |
107 ms |
11856 KB |
Output is correct |
2 |
Correct |
106 ms |
11980 KB |
Output is correct |
3 |
Correct |
110 ms |
11876 KB |
Output is correct |
4 |
Correct |
115 ms |
11864 KB |
Output is correct |
5 |
Correct |
106 ms |
11860 KB |
Output is correct |
6 |
Correct |
104 ms |
11864 KB |
Output is correct |
7 |
Correct |
128 ms |
11832 KB |
Output is correct |
8 |
Correct |
113 ms |
11876 KB |
Output is correct |
9 |
Correct |
107 ms |
11812 KB |
Output is correct |
10 |
Correct |
107 ms |
11852 KB |
Output is correct |
11 |
Correct |
105 ms |
11768 KB |
Output is correct |
12 |
Correct |
111 ms |
11852 KB |
Output is correct |
13 |
Correct |
109 ms |
11868 KB |
Output is correct |
14 |
Correct |
105 ms |
11868 KB |
Output is correct |
15 |
Correct |
105 ms |
11860 KB |
Output is correct |
16 |
Correct |
122 ms |
11948 KB |
Output is correct |
17 |
Correct |
109 ms |
11864 KB |
Output is correct |
18 |
Correct |
108 ms |
11872 KB |
Output is correct |
19 |
Correct |
105 ms |
11876 KB |
Output is correct |
20 |
Correct |
105 ms |
11832 KB |
Output is correct |
21 |
Correct |
111 ms |
11876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
11856 KB |
Output is correct |
2 |
Correct |
106 ms |
11980 KB |
Output is correct |
3 |
Correct |
110 ms |
11876 KB |
Output is correct |
4 |
Correct |
115 ms |
11864 KB |
Output is correct |
5 |
Correct |
106 ms |
11860 KB |
Output is correct |
6 |
Correct |
104 ms |
11864 KB |
Output is correct |
7 |
Correct |
128 ms |
11832 KB |
Output is correct |
8 |
Correct |
113 ms |
11876 KB |
Output is correct |
9 |
Correct |
107 ms |
11812 KB |
Output is correct |
10 |
Correct |
107 ms |
11852 KB |
Output is correct |
11 |
Correct |
105 ms |
11768 KB |
Output is correct |
12 |
Correct |
111 ms |
11852 KB |
Output is correct |
13 |
Correct |
109 ms |
11868 KB |
Output is correct |
14 |
Correct |
105 ms |
11868 KB |
Output is correct |
15 |
Correct |
105 ms |
11860 KB |
Output is correct |
16 |
Correct |
122 ms |
11948 KB |
Output is correct |
17 |
Correct |
109 ms |
11864 KB |
Output is correct |
18 |
Correct |
108 ms |
11872 KB |
Output is correct |
19 |
Correct |
105 ms |
11876 KB |
Output is correct |
20 |
Correct |
105 ms |
11832 KB |
Output is correct |
21 |
Correct |
111 ms |
11876 KB |
Output is correct |
22 |
Correct |
242 ms |
11852 KB |
Output is correct |
23 |
Correct |
238 ms |
11876 KB |
Output is correct |
24 |
Correct |
252 ms |
11968 KB |
Output is correct |
25 |
Correct |
238 ms |
11840 KB |
Output is correct |
26 |
Correct |
244 ms |
11840 KB |
Output is correct |
27 |
Correct |
249 ms |
11868 KB |
Output is correct |
28 |
Correct |
244 ms |
11852 KB |
Output is correct |
29 |
Correct |
265 ms |
11740 KB |
Output is correct |
30 |
Correct |
237 ms |
11968 KB |
Output is correct |
31 |
Correct |
246 ms |
11840 KB |
Output is correct |
32 |
Correct |
267 ms |
11844 KB |
Output is correct |
33 |
Correct |
242 ms |
11728 KB |
Output is correct |
34 |
Correct |
250 ms |
11740 KB |
Output is correct |
35 |
Correct |
256 ms |
11728 KB |
Output is correct |
36 |
Correct |
239 ms |
11968 KB |
Output is correct |
37 |
Correct |
251 ms |
11848 KB |
Output is correct |
38 |
Correct |
241 ms |
11724 KB |
Output is correct |
39 |
Correct |
267 ms |
11728 KB |
Output is correct |
40 |
Correct |
239 ms |
11908 KB |
Output is correct |
41 |
Correct |
247 ms |
11860 KB |
Output is correct |
42 |
Correct |
252 ms |
11868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
11856 KB |
Output is correct |
2 |
Correct |
106 ms |
11980 KB |
Output is correct |
3 |
Correct |
110 ms |
11876 KB |
Output is correct |
4 |
Correct |
115 ms |
11864 KB |
Output is correct |
5 |
Correct |
106 ms |
11860 KB |
Output is correct |
6 |
Correct |
104 ms |
11864 KB |
Output is correct |
7 |
Correct |
128 ms |
11832 KB |
Output is correct |
8 |
Correct |
113 ms |
11876 KB |
Output is correct |
9 |
Correct |
107 ms |
11812 KB |
Output is correct |
10 |
Correct |
107 ms |
11852 KB |
Output is correct |
11 |
Correct |
105 ms |
11768 KB |
Output is correct |
12 |
Correct |
111 ms |
11852 KB |
Output is correct |
13 |
Correct |
109 ms |
11868 KB |
Output is correct |
14 |
Correct |
105 ms |
11868 KB |
Output is correct |
15 |
Correct |
105 ms |
11860 KB |
Output is correct |
16 |
Correct |
122 ms |
11948 KB |
Output is correct |
17 |
Correct |
109 ms |
11864 KB |
Output is correct |
18 |
Correct |
108 ms |
11872 KB |
Output is correct |
19 |
Correct |
105 ms |
11876 KB |
Output is correct |
20 |
Correct |
105 ms |
11832 KB |
Output is correct |
21 |
Correct |
111 ms |
11876 KB |
Output is correct |
22 |
Correct |
242 ms |
11852 KB |
Output is correct |
23 |
Correct |
238 ms |
11876 KB |
Output is correct |
24 |
Correct |
252 ms |
11968 KB |
Output is correct |
25 |
Correct |
238 ms |
11840 KB |
Output is correct |
26 |
Correct |
244 ms |
11840 KB |
Output is correct |
27 |
Correct |
249 ms |
11868 KB |
Output is correct |
28 |
Correct |
244 ms |
11852 KB |
Output is correct |
29 |
Correct |
265 ms |
11740 KB |
Output is correct |
30 |
Correct |
237 ms |
11968 KB |
Output is correct |
31 |
Correct |
246 ms |
11840 KB |
Output is correct |
32 |
Correct |
267 ms |
11844 KB |
Output is correct |
33 |
Correct |
242 ms |
11728 KB |
Output is correct |
34 |
Correct |
250 ms |
11740 KB |
Output is correct |
35 |
Correct |
256 ms |
11728 KB |
Output is correct |
36 |
Correct |
239 ms |
11968 KB |
Output is correct |
37 |
Correct |
251 ms |
11848 KB |
Output is correct |
38 |
Correct |
241 ms |
11724 KB |
Output is correct |
39 |
Correct |
267 ms |
11728 KB |
Output is correct |
40 |
Correct |
239 ms |
11908 KB |
Output is correct |
41 |
Correct |
247 ms |
11860 KB |
Output is correct |
42 |
Correct |
252 ms |
11868 KB |
Output is correct |
43 |
Execution timed out |
1189 ms |
14720 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
11856 KB |
Output is correct |
2 |
Correct |
106 ms |
11980 KB |
Output is correct |
3 |
Correct |
110 ms |
11876 KB |
Output is correct |
4 |
Correct |
115 ms |
11864 KB |
Output is correct |
5 |
Correct |
106 ms |
11860 KB |
Output is correct |
6 |
Correct |
104 ms |
11864 KB |
Output is correct |
7 |
Correct |
128 ms |
11832 KB |
Output is correct |
8 |
Correct |
113 ms |
11876 KB |
Output is correct |
9 |
Correct |
107 ms |
11812 KB |
Output is correct |
10 |
Correct |
107 ms |
11852 KB |
Output is correct |
11 |
Correct |
105 ms |
11768 KB |
Output is correct |
12 |
Correct |
111 ms |
11852 KB |
Output is correct |
13 |
Correct |
109 ms |
11868 KB |
Output is correct |
14 |
Correct |
105 ms |
11868 KB |
Output is correct |
15 |
Correct |
105 ms |
11860 KB |
Output is correct |
16 |
Correct |
122 ms |
11948 KB |
Output is correct |
17 |
Correct |
109 ms |
11864 KB |
Output is correct |
18 |
Correct |
108 ms |
11872 KB |
Output is correct |
19 |
Correct |
105 ms |
11876 KB |
Output is correct |
20 |
Correct |
105 ms |
11832 KB |
Output is correct |
21 |
Correct |
111 ms |
11876 KB |
Output is correct |
22 |
Correct |
242 ms |
11852 KB |
Output is correct |
23 |
Correct |
238 ms |
11876 KB |
Output is correct |
24 |
Correct |
252 ms |
11968 KB |
Output is correct |
25 |
Correct |
238 ms |
11840 KB |
Output is correct |
26 |
Correct |
244 ms |
11840 KB |
Output is correct |
27 |
Correct |
249 ms |
11868 KB |
Output is correct |
28 |
Correct |
244 ms |
11852 KB |
Output is correct |
29 |
Correct |
265 ms |
11740 KB |
Output is correct |
30 |
Correct |
237 ms |
11968 KB |
Output is correct |
31 |
Correct |
246 ms |
11840 KB |
Output is correct |
32 |
Correct |
267 ms |
11844 KB |
Output is correct |
33 |
Correct |
242 ms |
11728 KB |
Output is correct |
34 |
Correct |
250 ms |
11740 KB |
Output is correct |
35 |
Correct |
256 ms |
11728 KB |
Output is correct |
36 |
Correct |
239 ms |
11968 KB |
Output is correct |
37 |
Correct |
251 ms |
11848 KB |
Output is correct |
38 |
Correct |
241 ms |
11724 KB |
Output is correct |
39 |
Correct |
267 ms |
11728 KB |
Output is correct |
40 |
Correct |
239 ms |
11908 KB |
Output is correct |
41 |
Correct |
247 ms |
11860 KB |
Output is correct |
42 |
Correct |
252 ms |
11868 KB |
Output is correct |
43 |
Execution timed out |
1189 ms |
14720 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |