// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll;
#define ff first
#define ss second
#define pb emplace_back
#define AI(x) begin(x),end(x)
template<class I>bool chmax(I&a,I b){return a<b?(a=b,true):false;}
template<class I>bool chmin(I&a,I b){return b<a?(a=b,true):false;}
#ifdef OWO
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
#else
#pragma GCC optimize("Ofast")
#define debug(...) ((void)0)
#endif
const int kC = 62;
const int MOD = 998244353;
inline int ci(char c){ return c >= 'a' ? c - 'a' : (c >= 'A' ? c - 'A' + 26 : c - '0' + 52); }
inline void mad(int &a, int b){ a += b - MOD; a += (a>>31) & MOD; }
int adj[kC][kC];
int tri[kC][kC][kC];
void init(){
memset(adj, 0, sizeof(adj));
memset(tri, 0, sizeof(tri));
}
bool isPalim(string s){
int n = s.size();
for(int l = 0, r = n-1; l < r; ++l, --r)
if(s[l] != s[r]) return false;
return true;
}
constexpr int perm(int i, int j, int k, int l){
if(i == j and j == k and k == l) return 1;
if(j == k) return (i == j or k == l ? 4 : 12);
if(i == j and k == l) return 6;
if(i == j or k == l) return 12;
return 24;
}
int solve(vector<string> &s){
debug(s);
if(s.empty()) return 0;
init();
set<string> ss;
for(auto i: s){
if(ss.count(i)) continue;
int a = ci(i.front()), b = ci(i.back());
ss.insert(i);
++adj[a][b];
if(isPalim(i)) continue;
reverse(AI(i));
ss.insert(i);
++adj[b][a];
}
// for(int i = 0; i < kC; ++i){
// for(int j = 0; j < kC; ++j)
// cout << adj[i][j] << ' ';
// cout << '\n';
// }
// construct tri
for(int u = 0; u < kC; ++u)
for(int i = 0; i < kC; ++i) if(adj[u][i])
for(int j = i; j < kC; ++j) if(adj[u][j])
for(int k = j; k < kC; ++k) if(adj[u][k]){
int v = 1ll * adj[i][u] * adj[j][u] * adj[k][u] % MOD;
mad(tri[i][j][k], v);
}
int ans = 0;
for(int i = 0; i < kC; ++i)
for(int j = i; j < kC; ++j)
for(int k = j; k < kC; ++k) if(tri[i][j][k])
for(int l = k; l < kC; ++l){
ll v = 1ll * tri[i][j][k] * tri[i][j][l] % MOD;
ll u = 1ll * tri[i][k][l] * tri[j][k][l] % MOD;
v = v * u % MOD;
mad(ans, v * perm(i, j, k, l) % MOD);
}
debug(ans);
return ans;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n; cin >> n; cin.ignore();
vector<vector<string>> bkt(11);
string s;
for(int i = 1; i <= n; ++i){
getline(cin, s);
bkt[s.length()].push_back(s);
}
int ans = 0;
for(int i = 3; i <= 10; ++i)
mad(ans, solve(bkt[i]));
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
118 ms |
7404 KB |
Output is correct |
2 |
Correct |
106 ms |
7500 KB |
Output is correct |
3 |
Correct |
130 ms |
7404 KB |
Output is correct |
4 |
Correct |
103 ms |
7452 KB |
Output is correct |
5 |
Correct |
102 ms |
7356 KB |
Output is correct |
6 |
Correct |
112 ms |
7360 KB |
Output is correct |
7 |
Correct |
106 ms |
7452 KB |
Output is correct |
8 |
Correct |
106 ms |
7464 KB |
Output is correct |
9 |
Correct |
107 ms |
7480 KB |
Output is correct |
10 |
Correct |
105 ms |
7344 KB |
Output is correct |
11 |
Correct |
104 ms |
7400 KB |
Output is correct |
12 |
Correct |
120 ms |
7508 KB |
Output is correct |
13 |
Correct |
105 ms |
7368 KB |
Output is correct |
14 |
Correct |
117 ms |
7388 KB |
Output is correct |
15 |
Correct |
126 ms |
7456 KB |
Output is correct |
16 |
Correct |
121 ms |
7368 KB |
Output is correct |
17 |
Correct |
105 ms |
7568 KB |
Output is correct |
18 |
Correct |
111 ms |
7460 KB |
Output is correct |
19 |
Correct |
106 ms |
7352 KB |
Output is correct |
20 |
Correct |
109 ms |
7352 KB |
Output is correct |
21 |
Correct |
105 ms |
7368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
118 ms |
7404 KB |
Output is correct |
2 |
Correct |
106 ms |
7500 KB |
Output is correct |
3 |
Correct |
130 ms |
7404 KB |
Output is correct |
4 |
Correct |
103 ms |
7452 KB |
Output is correct |
5 |
Correct |
102 ms |
7356 KB |
Output is correct |
6 |
Correct |
112 ms |
7360 KB |
Output is correct |
7 |
Correct |
106 ms |
7452 KB |
Output is correct |
8 |
Correct |
106 ms |
7464 KB |
Output is correct |
9 |
Correct |
107 ms |
7480 KB |
Output is correct |
10 |
Correct |
105 ms |
7344 KB |
Output is correct |
11 |
Correct |
104 ms |
7400 KB |
Output is correct |
12 |
Correct |
120 ms |
7508 KB |
Output is correct |
13 |
Correct |
105 ms |
7368 KB |
Output is correct |
14 |
Correct |
117 ms |
7388 KB |
Output is correct |
15 |
Correct |
126 ms |
7456 KB |
Output is correct |
16 |
Correct |
121 ms |
7368 KB |
Output is correct |
17 |
Correct |
105 ms |
7568 KB |
Output is correct |
18 |
Correct |
111 ms |
7460 KB |
Output is correct |
19 |
Correct |
106 ms |
7352 KB |
Output is correct |
20 |
Correct |
109 ms |
7352 KB |
Output is correct |
21 |
Correct |
105 ms |
7368 KB |
Output is correct |
22 |
Correct |
108 ms |
6528 KB |
Output is correct |
23 |
Correct |
115 ms |
6616 KB |
Output is correct |
24 |
Correct |
110 ms |
6612 KB |
Output is correct |
25 |
Correct |
110 ms |
6712 KB |
Output is correct |
26 |
Correct |
110 ms |
6604 KB |
Output is correct |
27 |
Correct |
106 ms |
6500 KB |
Output is correct |
28 |
Correct |
108 ms |
6600 KB |
Output is correct |
29 |
Correct |
106 ms |
6624 KB |
Output is correct |
30 |
Correct |
110 ms |
6604 KB |
Output is correct |
31 |
Correct |
112 ms |
6612 KB |
Output is correct |
32 |
Correct |
107 ms |
6580 KB |
Output is correct |
33 |
Correct |
118 ms |
6644 KB |
Output is correct |
34 |
Correct |
118 ms |
6616 KB |
Output is correct |
35 |
Correct |
133 ms |
6576 KB |
Output is correct |
36 |
Correct |
117 ms |
6604 KB |
Output is correct |
37 |
Correct |
119 ms |
6656 KB |
Output is correct |
38 |
Correct |
120 ms |
6736 KB |
Output is correct |
39 |
Correct |
117 ms |
6564 KB |
Output is correct |
40 |
Correct |
110 ms |
6616 KB |
Output is correct |
41 |
Correct |
106 ms |
6628 KB |
Output is correct |
42 |
Correct |
113 ms |
6644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
118 ms |
7404 KB |
Output is correct |
2 |
Correct |
106 ms |
7500 KB |
Output is correct |
3 |
Correct |
130 ms |
7404 KB |
Output is correct |
4 |
Correct |
103 ms |
7452 KB |
Output is correct |
5 |
Correct |
102 ms |
7356 KB |
Output is correct |
6 |
Correct |
112 ms |
7360 KB |
Output is correct |
7 |
Correct |
106 ms |
7452 KB |
Output is correct |
8 |
Correct |
106 ms |
7464 KB |
Output is correct |
9 |
Correct |
107 ms |
7480 KB |
Output is correct |
10 |
Correct |
105 ms |
7344 KB |
Output is correct |
11 |
Correct |
104 ms |
7400 KB |
Output is correct |
12 |
Correct |
120 ms |
7508 KB |
Output is correct |
13 |
Correct |
105 ms |
7368 KB |
Output is correct |
14 |
Correct |
117 ms |
7388 KB |
Output is correct |
15 |
Correct |
126 ms |
7456 KB |
Output is correct |
16 |
Correct |
121 ms |
7368 KB |
Output is correct |
17 |
Correct |
105 ms |
7568 KB |
Output is correct |
18 |
Correct |
111 ms |
7460 KB |
Output is correct |
19 |
Correct |
106 ms |
7352 KB |
Output is correct |
20 |
Correct |
109 ms |
7352 KB |
Output is correct |
21 |
Correct |
105 ms |
7368 KB |
Output is correct |
22 |
Correct |
108 ms |
6528 KB |
Output is correct |
23 |
Correct |
115 ms |
6616 KB |
Output is correct |
24 |
Correct |
110 ms |
6612 KB |
Output is correct |
25 |
Correct |
110 ms |
6712 KB |
Output is correct |
26 |
Correct |
110 ms |
6604 KB |
Output is correct |
27 |
Correct |
106 ms |
6500 KB |
Output is correct |
28 |
Correct |
108 ms |
6600 KB |
Output is correct |
29 |
Correct |
106 ms |
6624 KB |
Output is correct |
30 |
Correct |
110 ms |
6604 KB |
Output is correct |
31 |
Correct |
112 ms |
6612 KB |
Output is correct |
32 |
Correct |
107 ms |
6580 KB |
Output is correct |
33 |
Correct |
118 ms |
6644 KB |
Output is correct |
34 |
Correct |
118 ms |
6616 KB |
Output is correct |
35 |
Correct |
133 ms |
6576 KB |
Output is correct |
36 |
Correct |
117 ms |
6604 KB |
Output is correct |
37 |
Correct |
119 ms |
6656 KB |
Output is correct |
38 |
Correct |
120 ms |
6736 KB |
Output is correct |
39 |
Correct |
117 ms |
6564 KB |
Output is correct |
40 |
Correct |
110 ms |
6616 KB |
Output is correct |
41 |
Correct |
106 ms |
6628 KB |
Output is correct |
42 |
Correct |
113 ms |
6644 KB |
Output is correct |
43 |
Correct |
138 ms |
6564 KB |
Output is correct |
44 |
Correct |
133 ms |
6616 KB |
Output is correct |
45 |
Correct |
133 ms |
6424 KB |
Output is correct |
46 |
Correct |
129 ms |
6644 KB |
Output is correct |
47 |
Correct |
131 ms |
6532 KB |
Output is correct |
48 |
Correct |
138 ms |
6356 KB |
Output is correct |
49 |
Correct |
131 ms |
6408 KB |
Output is correct |
50 |
Correct |
133 ms |
6444 KB |
Output is correct |
51 |
Correct |
131 ms |
6452 KB |
Output is correct |
52 |
Correct |
149 ms |
6432 KB |
Output is correct |
53 |
Correct |
139 ms |
6444 KB |
Output is correct |
54 |
Correct |
131 ms |
6504 KB |
Output is correct |
55 |
Correct |
128 ms |
6316 KB |
Output is correct |
56 |
Correct |
140 ms |
6628 KB |
Output is correct |
57 |
Correct |
132 ms |
6428 KB |
Output is correct |
58 |
Correct |
128 ms |
6428 KB |
Output is correct |
59 |
Correct |
129 ms |
6544 KB |
Output is correct |
60 |
Correct |
128 ms |
6328 KB |
Output is correct |
61 |
Correct |
126 ms |
6448 KB |
Output is correct |
62 |
Correct |
127 ms |
6412 KB |
Output is correct |
63 |
Correct |
138 ms |
6428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
118 ms |
7404 KB |
Output is correct |
2 |
Correct |
106 ms |
7500 KB |
Output is correct |
3 |
Correct |
130 ms |
7404 KB |
Output is correct |
4 |
Correct |
103 ms |
7452 KB |
Output is correct |
5 |
Correct |
102 ms |
7356 KB |
Output is correct |
6 |
Correct |
112 ms |
7360 KB |
Output is correct |
7 |
Correct |
106 ms |
7452 KB |
Output is correct |
8 |
Correct |
106 ms |
7464 KB |
Output is correct |
9 |
Correct |
107 ms |
7480 KB |
Output is correct |
10 |
Correct |
105 ms |
7344 KB |
Output is correct |
11 |
Correct |
104 ms |
7400 KB |
Output is correct |
12 |
Correct |
120 ms |
7508 KB |
Output is correct |
13 |
Correct |
105 ms |
7368 KB |
Output is correct |
14 |
Correct |
117 ms |
7388 KB |
Output is correct |
15 |
Correct |
126 ms |
7456 KB |
Output is correct |
16 |
Correct |
121 ms |
7368 KB |
Output is correct |
17 |
Correct |
105 ms |
7568 KB |
Output is correct |
18 |
Correct |
111 ms |
7460 KB |
Output is correct |
19 |
Correct |
106 ms |
7352 KB |
Output is correct |
20 |
Correct |
109 ms |
7352 KB |
Output is correct |
21 |
Correct |
105 ms |
7368 KB |
Output is correct |
22 |
Correct |
108 ms |
6528 KB |
Output is correct |
23 |
Correct |
115 ms |
6616 KB |
Output is correct |
24 |
Correct |
110 ms |
6612 KB |
Output is correct |
25 |
Correct |
110 ms |
6712 KB |
Output is correct |
26 |
Correct |
110 ms |
6604 KB |
Output is correct |
27 |
Correct |
106 ms |
6500 KB |
Output is correct |
28 |
Correct |
108 ms |
6600 KB |
Output is correct |
29 |
Correct |
106 ms |
6624 KB |
Output is correct |
30 |
Correct |
110 ms |
6604 KB |
Output is correct |
31 |
Correct |
112 ms |
6612 KB |
Output is correct |
32 |
Correct |
107 ms |
6580 KB |
Output is correct |
33 |
Correct |
118 ms |
6644 KB |
Output is correct |
34 |
Correct |
118 ms |
6616 KB |
Output is correct |
35 |
Correct |
133 ms |
6576 KB |
Output is correct |
36 |
Correct |
117 ms |
6604 KB |
Output is correct |
37 |
Correct |
119 ms |
6656 KB |
Output is correct |
38 |
Correct |
120 ms |
6736 KB |
Output is correct |
39 |
Correct |
117 ms |
6564 KB |
Output is correct |
40 |
Correct |
110 ms |
6616 KB |
Output is correct |
41 |
Correct |
106 ms |
6628 KB |
Output is correct |
42 |
Correct |
113 ms |
6644 KB |
Output is correct |
43 |
Correct |
138 ms |
6564 KB |
Output is correct |
44 |
Correct |
133 ms |
6616 KB |
Output is correct |
45 |
Correct |
133 ms |
6424 KB |
Output is correct |
46 |
Correct |
129 ms |
6644 KB |
Output is correct |
47 |
Correct |
131 ms |
6532 KB |
Output is correct |
48 |
Correct |
138 ms |
6356 KB |
Output is correct |
49 |
Correct |
131 ms |
6408 KB |
Output is correct |
50 |
Correct |
133 ms |
6444 KB |
Output is correct |
51 |
Correct |
131 ms |
6452 KB |
Output is correct |
52 |
Correct |
149 ms |
6432 KB |
Output is correct |
53 |
Correct |
139 ms |
6444 KB |
Output is correct |
54 |
Correct |
131 ms |
6504 KB |
Output is correct |
55 |
Correct |
128 ms |
6316 KB |
Output is correct |
56 |
Correct |
140 ms |
6628 KB |
Output is correct |
57 |
Correct |
132 ms |
6428 KB |
Output is correct |
58 |
Correct |
128 ms |
6428 KB |
Output is correct |
59 |
Correct |
129 ms |
6544 KB |
Output is correct |
60 |
Correct |
128 ms |
6328 KB |
Output is correct |
61 |
Correct |
126 ms |
6448 KB |
Output is correct |
62 |
Correct |
127 ms |
6412 KB |
Output is correct |
63 |
Correct |
138 ms |
6428 KB |
Output is correct |
64 |
Correct |
251 ms |
6404 KB |
Output is correct |
65 |
Correct |
302 ms |
7140 KB |
Output is correct |
66 |
Correct |
246 ms |
7008 KB |
Output is correct |
67 |
Correct |
238 ms |
7340 KB |
Output is correct |
68 |
Correct |
243 ms |
7296 KB |
Output is correct |
69 |
Correct |
241 ms |
7188 KB |
Output is correct |
70 |
Correct |
240 ms |
7140 KB |
Output is correct |
71 |
Correct |
239 ms |
7216 KB |
Output is correct |
72 |
Correct |
242 ms |
7200 KB |
Output is correct |
73 |
Correct |
240 ms |
7132 KB |
Output is correct |
74 |
Correct |
243 ms |
7144 KB |
Output is correct |
75 |
Correct |
241 ms |
7128 KB |
Output is correct |
76 |
Correct |
242 ms |
7064 KB |
Output is correct |
77 |
Correct |
241 ms |
7132 KB |
Output is correct |
78 |
Correct |
235 ms |
7128 KB |
Output is correct |
79 |
Correct |
273 ms |
7148 KB |
Output is correct |
80 |
Correct |
243 ms |
7120 KB |
Output is correct |
81 |
Correct |
242 ms |
7340 KB |
Output is correct |
82 |
Correct |
240 ms |
7112 KB |
Output is correct |
83 |
Correct |
246 ms |
7340 KB |
Output is correct |
84 |
Correct |
239 ms |
7344 KB |
Output is correct |