#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<unordered_set>
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
const int N = 1e5 + 9, mod = 998244353;
int n;
int a[11][62][62][2];
bool palind(string s) {
int l(0), r(s.size() - 1);
while (l < r) {
if (s[l] != s[r]) return false;
++l, --r;
}
return true;
}
int num(char c) {
if ('a' <= c && c <= 'z') return c - 'a';
if ('A' <= c && c <= 'Z') return c - 'A' + 26;
if ('0' <= c && c <= '9') return c - '0' + 52;
return -1;
}
ll calc(int len, int i, int j) {
if (i != j) {
if (i > j) swap(i, j);
return a[len][i][j][0];
}
return (a[len][i][j][1] + a[len][i][j][0] * 2) % mod;
}
ll dp[2][62][62][62][62];
void reset() {
for (int i1 = 0; i1 < n; ++i1)
for (int i2 = 0; i2 < n; ++i2)
for (int i3 = 0; i3 < n; ++i3)
for (int i4 = 0; i4 < n; ++i4) {
dp[0][i1][i2][i3][i4] = dp[1][i1][i2][i3][i4];
dp[1][i1][i2][i3][i4] = 0;
}
}
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n;
unordered_set<string> S;
vector<int> ch;
for (int ii = 0; ii < n; ++ii) {
string s; cin >> s;
if (S.count(s)) continue;
int i = num(s[0]), j = num(s.back());
if (i > j) swap(i, j);
ch.push_back(i);
ch.push_back(j);
++a[s.size()][i][j][palind(s)];
reverse(all(s));
S.insert(s);
}
sort(all(ch));
ch.resize(unique(all(ch)) - ch.begin());
n = ch.size();
for (int len = 3; len <= 10; ++len)
for (int i = 0; i < n; ++i)
for (int j = i; j < n; ++j) {
int i1 = ch[i], i2 = ch[j];
a[len][i][j][0] = a[len][i1][i2][0];
a[len][i][j][1] = a[len][i1][i2][1];
}
ll ans(0);
for (int len = 3; len <= 10; ++len) {
for (int i1 = 0; i1 < n; ++i1)
for (int i2 = 0; i2 < n; ++i2)
for (int i3 = 0; i3 < n; ++i3)
for (int i4 = 0; i4 < n; ++i4)
dp[1][i1][i2][i3][i4] = calc(len, i1, i2) * calc(len, i2, i3) % mod * calc(len, i3, i4) % mod * calc(len, i4, i1) % mod;
reset();
for (int i5 = 0; i5 < n; ++i5)
for (int i1 = 0; i1 < n; ++i1)
for (int i2 = 0; i2 < n; ++i2)
for (int i3 = 0; i3 < n; ++i3)
for (int i4 = 0; i4 < n; ++i4) if (dp[0][i1][i2][i3][i4])
(dp[1][i5][i2][i3][i4] += dp[0][i1][i2][i3][i4] * calc(len, i1, i5)) %= mod;
reset();
for (int i6 = 0; i6 < n; ++i6)
for (int i5 = 0; i5 < n; ++i5)
for (int i2 = 0; i2 < n; ++i2)
for (int i3 = 0; i3 < n; ++i3)
for (int i4 = 0; i4 < n; ++i4)
(dp[1][i5][i6][i3][i4] += dp[0][i5][i2][i3][i4] * calc(len, i2, i6) % mod * calc(len, i5, i6)) %= mod;
reset();
for (int i7 = 0; i7 < n; ++i7)
for (int i5 = 0; i5 < n; ++i5)
for (int i6 = 0; i6 < n; ++i6)
for (int i3 = 0; i3 < n; ++i3)
for (int i4 = 0; i4 < n; ++i4) if (dp[0][i5][i6][i3][i4])
(dp[1][i5][i6][i7][i4] += dp[0][i5][i6][i3][i4] * calc(len, i3, i7) % mod * calc(len, i6, i7)) %= mod;
reset();
for (int i8 = 0; i8 < n; ++i8)
for (int i5 = 0; i5 < n; ++i5)
for (int i6 = 0; i6 < n; ++i6)
for (int i7 = 0; i7 < n; ++i7)
for (int i4 = 0; i4 < n; ++i4)
(dp[1][i5][i6][i7][i8] += dp[0][i5][i6][i7][i4] * calc(len, i4, i8) % mod * calc(len, i7, i8) % mod * calc(len, i8, i5)) %= mod;
reset();
for (int i5 = 0; i5 < n; ++i5)
for (int i6 = 0; i6 < n; ++i6)
for (int i7 = 0; i7 < n; ++i7)
for (int i8 = 0; i8 < n; ++i8)
(ans += dp[0][i5][i6][i7][i8]) %= mod;
}
cout << ans << '\n';
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
/*
for (int len = 3; len <= 10; ++len)
for (int i1 = 0; i1 < n; ++i1)
for (int i2 = 0; i2 < n; ++i2)
for (int i3 = 0; i3 < n; ++i3)
for (int i4 = 0; i4 < n; ++i4)
for (int i5 = 0; i5 < n; ++i5)
for (int i6 = 0; i6 < n; ++i6)
for (int i7 = 0; i7 < n; ++i7)
for (int i8 = 0; i8 < n; ++i8) {
ans += calc(len, i1, i2) * calc(len, i2, i3) % mod * calc(len, i3, i4) % mod * calc(len, i4, i1) % mod
* calc(len, i1, i5) % mod * calc(len, i2, i6) % mod * calc(len, i3, i7) % mod * calc(len, i4, i8) % mod
* calc(len, i5, i6) % mod * calc(len, i6, i7) % mod * calc(len, i7, i8) % mod * calc(len, i8, i5);
ans %= mod;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
9528 KB |
Output is correct |
2 |
Correct |
47 ms |
9528 KB |
Output is correct |
3 |
Correct |
46 ms |
9512 KB |
Output is correct |
4 |
Correct |
65 ms |
9556 KB |
Output is correct |
5 |
Correct |
73 ms |
9520 KB |
Output is correct |
6 |
Correct |
57 ms |
9524 KB |
Output is correct |
7 |
Correct |
56 ms |
9568 KB |
Output is correct |
8 |
Correct |
54 ms |
9520 KB |
Output is correct |
9 |
Correct |
49 ms |
9648 KB |
Output is correct |
10 |
Correct |
50 ms |
9516 KB |
Output is correct |
11 |
Correct |
54 ms |
9524 KB |
Output is correct |
12 |
Correct |
62 ms |
9532 KB |
Output is correct |
13 |
Correct |
64 ms |
9516 KB |
Output is correct |
14 |
Correct |
52 ms |
9528 KB |
Output is correct |
15 |
Correct |
49 ms |
9632 KB |
Output is correct |
16 |
Correct |
48 ms |
9584 KB |
Output is correct |
17 |
Correct |
46 ms |
9520 KB |
Output is correct |
18 |
Correct |
59 ms |
9552 KB |
Output is correct |
19 |
Correct |
63 ms |
9512 KB |
Output is correct |
20 |
Correct |
48 ms |
9584 KB |
Output is correct |
21 |
Correct |
49 ms |
9512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
9528 KB |
Output is correct |
2 |
Correct |
47 ms |
9528 KB |
Output is correct |
3 |
Correct |
46 ms |
9512 KB |
Output is correct |
4 |
Correct |
65 ms |
9556 KB |
Output is correct |
5 |
Correct |
73 ms |
9520 KB |
Output is correct |
6 |
Correct |
57 ms |
9524 KB |
Output is correct |
7 |
Correct |
56 ms |
9568 KB |
Output is correct |
8 |
Correct |
54 ms |
9520 KB |
Output is correct |
9 |
Correct |
49 ms |
9648 KB |
Output is correct |
10 |
Correct |
50 ms |
9516 KB |
Output is correct |
11 |
Correct |
54 ms |
9524 KB |
Output is correct |
12 |
Correct |
62 ms |
9532 KB |
Output is correct |
13 |
Correct |
64 ms |
9516 KB |
Output is correct |
14 |
Correct |
52 ms |
9528 KB |
Output is correct |
15 |
Correct |
49 ms |
9632 KB |
Output is correct |
16 |
Correct |
48 ms |
9584 KB |
Output is correct |
17 |
Correct |
46 ms |
9520 KB |
Output is correct |
18 |
Correct |
59 ms |
9552 KB |
Output is correct |
19 |
Correct |
63 ms |
9512 KB |
Output is correct |
20 |
Correct |
48 ms |
9584 KB |
Output is correct |
21 |
Correct |
49 ms |
9512 KB |
Output is correct |
22 |
Correct |
240 ms |
15616 KB |
Output is correct |
23 |
Correct |
334 ms |
15608 KB |
Output is correct |
24 |
Correct |
242 ms |
15664 KB |
Output is correct |
25 |
Correct |
318 ms |
15612 KB |
Output is correct |
26 |
Correct |
233 ms |
15512 KB |
Output is correct |
27 |
Correct |
250 ms |
15532 KB |
Output is correct |
28 |
Correct |
274 ms |
15520 KB |
Output is correct |
29 |
Correct |
333 ms |
15624 KB |
Output is correct |
30 |
Correct |
241 ms |
15728 KB |
Output is correct |
31 |
Correct |
242 ms |
15612 KB |
Output is correct |
32 |
Correct |
245 ms |
15524 KB |
Output is correct |
33 |
Correct |
242 ms |
15604 KB |
Output is correct |
34 |
Correct |
233 ms |
15544 KB |
Output is correct |
35 |
Correct |
260 ms |
15612 KB |
Output is correct |
36 |
Correct |
235 ms |
15620 KB |
Output is correct |
37 |
Correct |
260 ms |
15612 KB |
Output is correct |
38 |
Correct |
242 ms |
15712 KB |
Output is correct |
39 |
Correct |
244 ms |
15612 KB |
Output is correct |
40 |
Correct |
286 ms |
15516 KB |
Output is correct |
41 |
Correct |
237 ms |
15608 KB |
Output is correct |
42 |
Correct |
260 ms |
15516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
9528 KB |
Output is correct |
2 |
Correct |
47 ms |
9528 KB |
Output is correct |
3 |
Correct |
46 ms |
9512 KB |
Output is correct |
4 |
Correct |
65 ms |
9556 KB |
Output is correct |
5 |
Correct |
73 ms |
9520 KB |
Output is correct |
6 |
Correct |
57 ms |
9524 KB |
Output is correct |
7 |
Correct |
56 ms |
9568 KB |
Output is correct |
8 |
Correct |
54 ms |
9520 KB |
Output is correct |
9 |
Correct |
49 ms |
9648 KB |
Output is correct |
10 |
Correct |
50 ms |
9516 KB |
Output is correct |
11 |
Correct |
54 ms |
9524 KB |
Output is correct |
12 |
Correct |
62 ms |
9532 KB |
Output is correct |
13 |
Correct |
64 ms |
9516 KB |
Output is correct |
14 |
Correct |
52 ms |
9528 KB |
Output is correct |
15 |
Correct |
49 ms |
9632 KB |
Output is correct |
16 |
Correct |
48 ms |
9584 KB |
Output is correct |
17 |
Correct |
46 ms |
9520 KB |
Output is correct |
18 |
Correct |
59 ms |
9552 KB |
Output is correct |
19 |
Correct |
63 ms |
9512 KB |
Output is correct |
20 |
Correct |
48 ms |
9584 KB |
Output is correct |
21 |
Correct |
49 ms |
9512 KB |
Output is correct |
22 |
Correct |
240 ms |
15616 KB |
Output is correct |
23 |
Correct |
334 ms |
15608 KB |
Output is correct |
24 |
Correct |
242 ms |
15664 KB |
Output is correct |
25 |
Correct |
318 ms |
15612 KB |
Output is correct |
26 |
Correct |
233 ms |
15512 KB |
Output is correct |
27 |
Correct |
250 ms |
15532 KB |
Output is correct |
28 |
Correct |
274 ms |
15520 KB |
Output is correct |
29 |
Correct |
333 ms |
15624 KB |
Output is correct |
30 |
Correct |
241 ms |
15728 KB |
Output is correct |
31 |
Correct |
242 ms |
15612 KB |
Output is correct |
32 |
Correct |
245 ms |
15524 KB |
Output is correct |
33 |
Correct |
242 ms |
15604 KB |
Output is correct |
34 |
Correct |
233 ms |
15544 KB |
Output is correct |
35 |
Correct |
260 ms |
15612 KB |
Output is correct |
36 |
Correct |
235 ms |
15620 KB |
Output is correct |
37 |
Correct |
260 ms |
15612 KB |
Output is correct |
38 |
Correct |
242 ms |
15712 KB |
Output is correct |
39 |
Correct |
244 ms |
15612 KB |
Output is correct |
40 |
Correct |
286 ms |
15516 KB |
Output is correct |
41 |
Correct |
237 ms |
15608 KB |
Output is correct |
42 |
Correct |
260 ms |
15516 KB |
Output is correct |
43 |
Execution timed out |
1172 ms |
49136 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
9528 KB |
Output is correct |
2 |
Correct |
47 ms |
9528 KB |
Output is correct |
3 |
Correct |
46 ms |
9512 KB |
Output is correct |
4 |
Correct |
65 ms |
9556 KB |
Output is correct |
5 |
Correct |
73 ms |
9520 KB |
Output is correct |
6 |
Correct |
57 ms |
9524 KB |
Output is correct |
7 |
Correct |
56 ms |
9568 KB |
Output is correct |
8 |
Correct |
54 ms |
9520 KB |
Output is correct |
9 |
Correct |
49 ms |
9648 KB |
Output is correct |
10 |
Correct |
50 ms |
9516 KB |
Output is correct |
11 |
Correct |
54 ms |
9524 KB |
Output is correct |
12 |
Correct |
62 ms |
9532 KB |
Output is correct |
13 |
Correct |
64 ms |
9516 KB |
Output is correct |
14 |
Correct |
52 ms |
9528 KB |
Output is correct |
15 |
Correct |
49 ms |
9632 KB |
Output is correct |
16 |
Correct |
48 ms |
9584 KB |
Output is correct |
17 |
Correct |
46 ms |
9520 KB |
Output is correct |
18 |
Correct |
59 ms |
9552 KB |
Output is correct |
19 |
Correct |
63 ms |
9512 KB |
Output is correct |
20 |
Correct |
48 ms |
9584 KB |
Output is correct |
21 |
Correct |
49 ms |
9512 KB |
Output is correct |
22 |
Correct |
240 ms |
15616 KB |
Output is correct |
23 |
Correct |
334 ms |
15608 KB |
Output is correct |
24 |
Correct |
242 ms |
15664 KB |
Output is correct |
25 |
Correct |
318 ms |
15612 KB |
Output is correct |
26 |
Correct |
233 ms |
15512 KB |
Output is correct |
27 |
Correct |
250 ms |
15532 KB |
Output is correct |
28 |
Correct |
274 ms |
15520 KB |
Output is correct |
29 |
Correct |
333 ms |
15624 KB |
Output is correct |
30 |
Correct |
241 ms |
15728 KB |
Output is correct |
31 |
Correct |
242 ms |
15612 KB |
Output is correct |
32 |
Correct |
245 ms |
15524 KB |
Output is correct |
33 |
Correct |
242 ms |
15604 KB |
Output is correct |
34 |
Correct |
233 ms |
15544 KB |
Output is correct |
35 |
Correct |
260 ms |
15612 KB |
Output is correct |
36 |
Correct |
235 ms |
15620 KB |
Output is correct |
37 |
Correct |
260 ms |
15612 KB |
Output is correct |
38 |
Correct |
242 ms |
15712 KB |
Output is correct |
39 |
Correct |
244 ms |
15612 KB |
Output is correct |
40 |
Correct |
286 ms |
15516 KB |
Output is correct |
41 |
Correct |
237 ms |
15608 KB |
Output is correct |
42 |
Correct |
260 ms |
15516 KB |
Output is correct |
43 |
Execution timed out |
1172 ms |
49136 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |