#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
class Solver {
constexpr static unsigned long long MOD = 998244353;
constexpr static int MAX_L = 10;
constexpr static int MAX_CHAR_ID = 62;
vector<string> words[MAX_L+1];
unsigned char rotations[1<<24];
inline int encode_char(char c) {
if(c >= 'a' && c <= 'z')
return c-'a';
else if(c >= 'A' && c <= 'Z')
return c-'A'+26;
else // if(c >= '0' && c <= '9')
return c-'0'+52;
}
inline bool unpack(int packed_seq, int * seq) {
for(int i = 0; i < 4; i++) {
seq[3-i] = packed_seq & 63;
if(seq[3-i] >= MAX_CHAR_ID) return false;
packed_seq >>= 6;
}
return true;
}
inline int pack(int * seq) {
int ret = 0;
for(int i = 0; i < 4; i++) ret = (ret << 6) | seq[i];
return ret;
}
unsigned long long solve(int L) {
unsigned int cnt_by_end_chars[MAX_CHAR_ID][MAX_CHAR_ID];
memset(cnt_by_end_chars, 0, sizeof(cnt_by_end_chars));
set<string> w;
for(string & word : words[L]) {
w.insert(word);
reverse(word.begin(), word.end());
w.insert(word);
}
for(const string & word : w) {
int start_char_id = encode_char(word[0]);
int end_char_id = encode_char(word[L-1]);
cnt_by_end_chars[start_char_id][end_char_id]++;
}
unsigned int cnt_by_vertices[MAX_CHAR_ID][MAX_CHAR_ID][MAX_CHAR_ID];
memset(cnt_by_vertices, 0, sizeof(cnt_by_vertices));
int seq[4];
for(int i = 0; i < (1<<18); i++) {
if(!unpack(i<<6, seq)) continue;
unsigned long long sum_cnt = 0;
for(int j = 0; j < MAX_CHAR_ID; j++) {
unsigned long long cnt = 1;
for(int k = 0; k < 3; k++)
cnt = cnt * cnt_by_end_chars[seq[k]][j];
sum_cnt += cnt;
if(sum_cnt >= 5*MOD*MOD) sum_cnt -= 5*MOD*MOD;
}
cnt_by_vertices[seq[0]][seq[1]][seq[2]] = sum_cnt % MOD;
}
unsigned long long ret = 0;
for(int i = 0; i < (1<<24); i++) if(rotations[i]) {
if(!unpack(i, seq)) continue;
unsigned long long cnt = rotations[i];
int V_seq[3];
for(int j = 0; j < 3; j++) V_seq[j] = seq[j];
for(int j = 0; j < 4; j++) {
cnt = cnt * cnt_by_vertices[V_seq[0]][V_seq[1]][V_seq[2]] % MOD;
if(j < 3) V_seq[2-j] = seq[3-j];
}
ret += cnt;
if(ret >= MOD) ret -= MOD;
}
return ret;
}
public:
Solver(vector<string> words_) {
for(int i = 0; i < (int)words_.size(); i++) {
string w = words_[i];
words[w.length()].push_back(w);
}
memset(rotations, 0, sizeof(rotations));
int seq_sorted[4];
for(int i = 0; i < (1<<24); i++) {
if(!unpack(i, seq_sorted)) continue;
for(int j = 0; j < 3; j++) for(int k = 1; k < 4; k++)
if(seq_sorted[j] > seq_sorted[k]) swap(seq_sorted[j], seq_sorted[k]);
rotations[pack(seq_sorted)]++;
}
}
unsigned long long solve() {
unsigned long long ret = 0;
for(int l = 2; l <= MAX_L; l++) {
ret += solve(l);
if(ret >= MOD) ret -= MOD;
}
return ret;
}
};
int main() {
cin.sync_with_stdio(0);
int N;
cin >> N;
vector<string> words(N);
for(int i = 0; i < N; i++) cin >> words[i];
Solver solver(words);
cout << solver.solve() << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
802 ms |
27152 KB |
Output is correct |
2 |
Correct |
799 ms |
26984 KB |
Output is correct |
3 |
Correct |
798 ms |
27136 KB |
Output is correct |
4 |
Correct |
797 ms |
27036 KB |
Output is correct |
5 |
Correct |
810 ms |
27108 KB |
Output is correct |
6 |
Correct |
808 ms |
27036 KB |
Output is correct |
7 |
Correct |
792 ms |
26976 KB |
Output is correct |
8 |
Correct |
789 ms |
27068 KB |
Output is correct |
9 |
Correct |
798 ms |
27148 KB |
Output is correct |
10 |
Correct |
802 ms |
27068 KB |
Output is correct |
11 |
Correct |
805 ms |
27028 KB |
Output is correct |
12 |
Correct |
830 ms |
27036 KB |
Output is correct |
13 |
Correct |
801 ms |
27052 KB |
Output is correct |
14 |
Correct |
796 ms |
26964 KB |
Output is correct |
15 |
Correct |
794 ms |
27052 KB |
Output is correct |
16 |
Correct |
802 ms |
27068 KB |
Output is correct |
17 |
Correct |
794 ms |
27160 KB |
Output is correct |
18 |
Correct |
800 ms |
27048 KB |
Output is correct |
19 |
Correct |
795 ms |
26980 KB |
Output is correct |
20 |
Correct |
802 ms |
26992 KB |
Output is correct |
21 |
Correct |
800 ms |
27128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
802 ms |
27152 KB |
Output is correct |
2 |
Correct |
799 ms |
26984 KB |
Output is correct |
3 |
Correct |
798 ms |
27136 KB |
Output is correct |
4 |
Correct |
797 ms |
27036 KB |
Output is correct |
5 |
Correct |
810 ms |
27108 KB |
Output is correct |
6 |
Correct |
808 ms |
27036 KB |
Output is correct |
7 |
Correct |
792 ms |
26976 KB |
Output is correct |
8 |
Correct |
789 ms |
27068 KB |
Output is correct |
9 |
Correct |
798 ms |
27148 KB |
Output is correct |
10 |
Correct |
802 ms |
27068 KB |
Output is correct |
11 |
Correct |
805 ms |
27028 KB |
Output is correct |
12 |
Correct |
830 ms |
27036 KB |
Output is correct |
13 |
Correct |
801 ms |
27052 KB |
Output is correct |
14 |
Correct |
796 ms |
26964 KB |
Output is correct |
15 |
Correct |
794 ms |
27052 KB |
Output is correct |
16 |
Correct |
802 ms |
27068 KB |
Output is correct |
17 |
Correct |
794 ms |
27160 KB |
Output is correct |
18 |
Correct |
800 ms |
27048 KB |
Output is correct |
19 |
Correct |
795 ms |
26980 KB |
Output is correct |
20 |
Correct |
802 ms |
26992 KB |
Output is correct |
21 |
Correct |
800 ms |
27128 KB |
Output is correct |
22 |
Correct |
792 ms |
26592 KB |
Output is correct |
23 |
Correct |
790 ms |
26308 KB |
Output is correct |
24 |
Correct |
777 ms |
26772 KB |
Output is correct |
25 |
Correct |
797 ms |
26344 KB |
Output is correct |
26 |
Correct |
792 ms |
26236 KB |
Output is correct |
27 |
Correct |
797 ms |
26640 KB |
Output is correct |
28 |
Correct |
795 ms |
26244 KB |
Output is correct |
29 |
Correct |
795 ms |
26564 KB |
Output is correct |
30 |
Correct |
799 ms |
26616 KB |
Output is correct |
31 |
Correct |
793 ms |
26372 KB |
Output is correct |
32 |
Correct |
790 ms |
26304 KB |
Output is correct |
33 |
Correct |
790 ms |
26608 KB |
Output is correct |
34 |
Correct |
799 ms |
26548 KB |
Output is correct |
35 |
Correct |
799 ms |
26316 KB |
Output is correct |
36 |
Correct |
782 ms |
26812 KB |
Output is correct |
37 |
Correct |
800 ms |
26196 KB |
Output is correct |
38 |
Correct |
786 ms |
26392 KB |
Output is correct |
39 |
Correct |
793 ms |
26300 KB |
Output is correct |
40 |
Correct |
786 ms |
26660 KB |
Output is correct |
41 |
Correct |
790 ms |
26300 KB |
Output is correct |
42 |
Correct |
789 ms |
26240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
802 ms |
27152 KB |
Output is correct |
2 |
Correct |
799 ms |
26984 KB |
Output is correct |
3 |
Correct |
798 ms |
27136 KB |
Output is correct |
4 |
Correct |
797 ms |
27036 KB |
Output is correct |
5 |
Correct |
810 ms |
27108 KB |
Output is correct |
6 |
Correct |
808 ms |
27036 KB |
Output is correct |
7 |
Correct |
792 ms |
26976 KB |
Output is correct |
8 |
Correct |
789 ms |
27068 KB |
Output is correct |
9 |
Correct |
798 ms |
27148 KB |
Output is correct |
10 |
Correct |
802 ms |
27068 KB |
Output is correct |
11 |
Correct |
805 ms |
27028 KB |
Output is correct |
12 |
Correct |
830 ms |
27036 KB |
Output is correct |
13 |
Correct |
801 ms |
27052 KB |
Output is correct |
14 |
Correct |
796 ms |
26964 KB |
Output is correct |
15 |
Correct |
794 ms |
27052 KB |
Output is correct |
16 |
Correct |
802 ms |
27068 KB |
Output is correct |
17 |
Correct |
794 ms |
27160 KB |
Output is correct |
18 |
Correct |
800 ms |
27048 KB |
Output is correct |
19 |
Correct |
795 ms |
26980 KB |
Output is correct |
20 |
Correct |
802 ms |
26992 KB |
Output is correct |
21 |
Correct |
800 ms |
27128 KB |
Output is correct |
22 |
Correct |
792 ms |
26592 KB |
Output is correct |
23 |
Correct |
790 ms |
26308 KB |
Output is correct |
24 |
Correct |
777 ms |
26772 KB |
Output is correct |
25 |
Correct |
797 ms |
26344 KB |
Output is correct |
26 |
Correct |
792 ms |
26236 KB |
Output is correct |
27 |
Correct |
797 ms |
26640 KB |
Output is correct |
28 |
Correct |
795 ms |
26244 KB |
Output is correct |
29 |
Correct |
795 ms |
26564 KB |
Output is correct |
30 |
Correct |
799 ms |
26616 KB |
Output is correct |
31 |
Correct |
793 ms |
26372 KB |
Output is correct |
32 |
Correct |
790 ms |
26304 KB |
Output is correct |
33 |
Correct |
790 ms |
26608 KB |
Output is correct |
34 |
Correct |
799 ms |
26548 KB |
Output is correct |
35 |
Correct |
799 ms |
26316 KB |
Output is correct |
36 |
Correct |
782 ms |
26812 KB |
Output is correct |
37 |
Correct |
800 ms |
26196 KB |
Output is correct |
38 |
Correct |
786 ms |
26392 KB |
Output is correct |
39 |
Correct |
793 ms |
26300 KB |
Output is correct |
40 |
Correct |
786 ms |
26660 KB |
Output is correct |
41 |
Correct |
790 ms |
26300 KB |
Output is correct |
42 |
Correct |
789 ms |
26240 KB |
Output is correct |
43 |
Correct |
790 ms |
26576 KB |
Output is correct |
44 |
Correct |
790 ms |
26416 KB |
Output is correct |
45 |
Correct |
785 ms |
26372 KB |
Output is correct |
46 |
Correct |
794 ms |
26328 KB |
Output is correct |
47 |
Correct |
791 ms |
26372 KB |
Output is correct |
48 |
Correct |
783 ms |
26368 KB |
Output is correct |
49 |
Correct |
780 ms |
26612 KB |
Output is correct |
50 |
Correct |
787 ms |
26364 KB |
Output is correct |
51 |
Correct |
786 ms |
26580 KB |
Output is correct |
52 |
Correct |
785 ms |
26380 KB |
Output is correct |
53 |
Correct |
787 ms |
26644 KB |
Output is correct |
54 |
Correct |
790 ms |
26596 KB |
Output is correct |
55 |
Correct |
791 ms |
26412 KB |
Output is correct |
56 |
Correct |
798 ms |
26736 KB |
Output is correct |
57 |
Correct |
804 ms |
26624 KB |
Output is correct |
58 |
Correct |
792 ms |
26328 KB |
Output is correct |
59 |
Correct |
801 ms |
26320 KB |
Output is correct |
60 |
Correct |
790 ms |
26324 KB |
Output is correct |
61 |
Correct |
791 ms |
26300 KB |
Output is correct |
62 |
Correct |
787 ms |
26732 KB |
Output is correct |
63 |
Correct |
784 ms |
26344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
802 ms |
27152 KB |
Output is correct |
2 |
Correct |
799 ms |
26984 KB |
Output is correct |
3 |
Correct |
798 ms |
27136 KB |
Output is correct |
4 |
Correct |
797 ms |
27036 KB |
Output is correct |
5 |
Correct |
810 ms |
27108 KB |
Output is correct |
6 |
Correct |
808 ms |
27036 KB |
Output is correct |
7 |
Correct |
792 ms |
26976 KB |
Output is correct |
8 |
Correct |
789 ms |
27068 KB |
Output is correct |
9 |
Correct |
798 ms |
27148 KB |
Output is correct |
10 |
Correct |
802 ms |
27068 KB |
Output is correct |
11 |
Correct |
805 ms |
27028 KB |
Output is correct |
12 |
Correct |
830 ms |
27036 KB |
Output is correct |
13 |
Correct |
801 ms |
27052 KB |
Output is correct |
14 |
Correct |
796 ms |
26964 KB |
Output is correct |
15 |
Correct |
794 ms |
27052 KB |
Output is correct |
16 |
Correct |
802 ms |
27068 KB |
Output is correct |
17 |
Correct |
794 ms |
27160 KB |
Output is correct |
18 |
Correct |
800 ms |
27048 KB |
Output is correct |
19 |
Correct |
795 ms |
26980 KB |
Output is correct |
20 |
Correct |
802 ms |
26992 KB |
Output is correct |
21 |
Correct |
800 ms |
27128 KB |
Output is correct |
22 |
Correct |
792 ms |
26592 KB |
Output is correct |
23 |
Correct |
790 ms |
26308 KB |
Output is correct |
24 |
Correct |
777 ms |
26772 KB |
Output is correct |
25 |
Correct |
797 ms |
26344 KB |
Output is correct |
26 |
Correct |
792 ms |
26236 KB |
Output is correct |
27 |
Correct |
797 ms |
26640 KB |
Output is correct |
28 |
Correct |
795 ms |
26244 KB |
Output is correct |
29 |
Correct |
795 ms |
26564 KB |
Output is correct |
30 |
Correct |
799 ms |
26616 KB |
Output is correct |
31 |
Correct |
793 ms |
26372 KB |
Output is correct |
32 |
Correct |
790 ms |
26304 KB |
Output is correct |
33 |
Correct |
790 ms |
26608 KB |
Output is correct |
34 |
Correct |
799 ms |
26548 KB |
Output is correct |
35 |
Correct |
799 ms |
26316 KB |
Output is correct |
36 |
Correct |
782 ms |
26812 KB |
Output is correct |
37 |
Correct |
800 ms |
26196 KB |
Output is correct |
38 |
Correct |
786 ms |
26392 KB |
Output is correct |
39 |
Correct |
793 ms |
26300 KB |
Output is correct |
40 |
Correct |
786 ms |
26660 KB |
Output is correct |
41 |
Correct |
790 ms |
26300 KB |
Output is correct |
42 |
Correct |
789 ms |
26240 KB |
Output is correct |
43 |
Correct |
790 ms |
26576 KB |
Output is correct |
44 |
Correct |
790 ms |
26416 KB |
Output is correct |
45 |
Correct |
785 ms |
26372 KB |
Output is correct |
46 |
Correct |
794 ms |
26328 KB |
Output is correct |
47 |
Correct |
791 ms |
26372 KB |
Output is correct |
48 |
Correct |
783 ms |
26368 KB |
Output is correct |
49 |
Correct |
780 ms |
26612 KB |
Output is correct |
50 |
Correct |
787 ms |
26364 KB |
Output is correct |
51 |
Correct |
786 ms |
26580 KB |
Output is correct |
52 |
Correct |
785 ms |
26380 KB |
Output is correct |
53 |
Correct |
787 ms |
26644 KB |
Output is correct |
54 |
Correct |
790 ms |
26596 KB |
Output is correct |
55 |
Correct |
791 ms |
26412 KB |
Output is correct |
56 |
Correct |
798 ms |
26736 KB |
Output is correct |
57 |
Correct |
804 ms |
26624 KB |
Output is correct |
58 |
Correct |
792 ms |
26328 KB |
Output is correct |
59 |
Correct |
801 ms |
26320 KB |
Output is correct |
60 |
Correct |
790 ms |
26324 KB |
Output is correct |
61 |
Correct |
791 ms |
26300 KB |
Output is correct |
62 |
Correct |
787 ms |
26732 KB |
Output is correct |
63 |
Correct |
784 ms |
26344 KB |
Output is correct |
64 |
Correct |
791 ms |
26688 KB |
Output is correct |
65 |
Correct |
787 ms |
26264 KB |
Output is correct |
66 |
Correct |
802 ms |
26356 KB |
Output is correct |
67 |
Correct |
804 ms |
26276 KB |
Output is correct |
68 |
Correct |
791 ms |
26580 KB |
Output is correct |
69 |
Correct |
783 ms |
26608 KB |
Output is correct |
70 |
Correct |
785 ms |
26596 KB |
Output is correct |
71 |
Correct |
791 ms |
26336 KB |
Output is correct |
72 |
Correct |
787 ms |
26356 KB |
Output is correct |
73 |
Correct |
785 ms |
26648 KB |
Output is correct |
74 |
Correct |
850 ms |
26256 KB |
Output is correct |
75 |
Correct |
858 ms |
26580 KB |
Output is correct |
76 |
Correct |
801 ms |
26564 KB |
Output is correct |
77 |
Correct |
793 ms |
26352 KB |
Output is correct |
78 |
Correct |
788 ms |
26456 KB |
Output is correct |
79 |
Correct |
786 ms |
26708 KB |
Output is correct |
80 |
Correct |
783 ms |
26716 KB |
Output is correct |
81 |
Correct |
788 ms |
26496 KB |
Output is correct |
82 |
Correct |
817 ms |
26400 KB |
Output is correct |
83 |
Correct |
787 ms |
26328 KB |
Output is correct |
84 |
Correct |
789 ms |
26732 KB |
Output is correct |