#include <bits/stdc++.h>
#define pb push_back
#define fst first
#define snd second
#define fore(i,a,b) for(ll i=a,isudgh=b;i<isudgh;i++)
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define mset(a,v) memset((a),(v),sizeof(a))
#define imp(v) {for(auto jhglkdfjg:v)cout<<jhglkdfjg<<" ";cout<<"\n";}
#define FIN ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
typedef int ll;
typedef long long LL;
typedef pair<ll,ll> ii;
// #pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
const ll MOD=998244353,MAXA=62;
ll add(ll a, ll b){a+=b;if(a>=MOD)a-=MOD;return a;}
ll sub(ll a, ll b){a-=b;if(a<0)a+=MOD;return a;}
ll mul(LL a, LL b){return a*b%MOD;}
ll A; //alphabet
vector<vector<ll>> g={
{},
{0},
{0},
{0},
{1,2},
{1,3},
{2,3},
{4,5,6}
};
ll q[62][62];
vector<ll>bor={0,0,0,1,0,1,2,4},tam={1,2,3,3,4,4,3,0,0};
vector<ll>pot(6);
vector<ll>dp[8];
ll get(ll i, ll j, ll h){
ll p=i-1-j;
ll ret=h/pot[p]%A;
// cout<<"get "<<i<<" "<<j<<" "<<h<<": "<<p<<": "<<ret<<"\n";
return ret;
}
ll go(ll i, ll h, ll c){
return (h*A+c)%pot[tam[i]];
}
ll solve(vector<string>&a){
if(!SZ(a))return 0;
// cout<<"solve "; imp(a);
ll n=SZ(a);
vector<char>c;
fore(i,0,n)c.pb(a[i][0]),c.pb(a[i].back());
sort(ALL(c));
vector<char> ci;
fore(i,0,SZ(c))if(!i||c[i]!=c[i-1])ci.pb(c[i]);
c=ci;
for(auto &s:a){
auto change=[&](char &j){
ll p=lower_bound(ALL(c),j)-c.begin();
j=p;
};
change(s[0]); change(s.back());
}
// imp(a);
A=SZ(c);
set<string> st[A][A];
for(auto s:a){
st[int(s[0])][int(s.back())].insert(s);
reverse(ALL(s));
st[int(s[0])][int(s.back())].insert(s);
}
fore(i,0,A)fore(j,0,A)q[i][j]=SZ(st[i][j]);
/*fore(i,0,A){
imp(q[i]);
}*/
pot[0]=1;
fore(i,1,6)pot[i]=pot[i-1]*A;
//dp
vector<ll>dp[2];
fore(i,0,2)dp[i].resize(pot[4]);
dp[8&1][0]=1;
for(ll i=7;i>=0;i--){
ll w=i&1;
fore(h,0,pot[(i?tam[i-1]:0)]){
ll &res=dp[w][h]; res=0;
fore(c,0,A){// pongo c
ll resi=1;
for(auto j:g[i]){
ll d=get(i,j,h);
resi=mul(resi,q[c][d]);
}
// cout<<i<<""
if(resi)res=add(res,mul(resi,dp[w^1][go(i,h,c)]));
}
}
// cout<<i<<" "<<h<<": "<<res<<"\n";
}
ll res=dp[0][0];
return res;
}
const ll MAXV=11;
int main(){FIN;
ll n; cin>>n;
vector<string> b[MAXV];
fore(i,0,n){
string s; cin>>s;
b[SZ(s)].pb(s);
}
ll res=0;
fore(i,0,MAXV){
res=add(res,solve(b[i]));
}
// res=solve(b[5]);
cout<<res<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
6580 KB |
Output is correct |
2 |
Correct |
53 ms |
7276 KB |
Output is correct |
3 |
Correct |
50 ms |
7464 KB |
Output is correct |
4 |
Correct |
51 ms |
7460 KB |
Output is correct |
5 |
Correct |
51 ms |
7260 KB |
Output is correct |
6 |
Correct |
53 ms |
7320 KB |
Output is correct |
7 |
Correct |
54 ms |
7460 KB |
Output is correct |
8 |
Correct |
52 ms |
7276 KB |
Output is correct |
9 |
Correct |
50 ms |
7252 KB |
Output is correct |
10 |
Correct |
55 ms |
7364 KB |
Output is correct |
11 |
Correct |
54 ms |
7412 KB |
Output is correct |
12 |
Correct |
55 ms |
7468 KB |
Output is correct |
13 |
Correct |
52 ms |
7432 KB |
Output is correct |
14 |
Correct |
52 ms |
7320 KB |
Output is correct |
15 |
Correct |
55 ms |
7468 KB |
Output is correct |
16 |
Correct |
53 ms |
7256 KB |
Output is correct |
17 |
Correct |
51 ms |
7464 KB |
Output is correct |
18 |
Correct |
55 ms |
7272 KB |
Output is correct |
19 |
Correct |
51 ms |
7452 KB |
Output is correct |
20 |
Correct |
54 ms |
7372 KB |
Output is correct |
21 |
Correct |
50 ms |
7244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
6580 KB |
Output is correct |
2 |
Correct |
53 ms |
7276 KB |
Output is correct |
3 |
Correct |
50 ms |
7464 KB |
Output is correct |
4 |
Correct |
51 ms |
7460 KB |
Output is correct |
5 |
Correct |
51 ms |
7260 KB |
Output is correct |
6 |
Correct |
53 ms |
7320 KB |
Output is correct |
7 |
Correct |
54 ms |
7460 KB |
Output is correct |
8 |
Correct |
52 ms |
7276 KB |
Output is correct |
9 |
Correct |
50 ms |
7252 KB |
Output is correct |
10 |
Correct |
55 ms |
7364 KB |
Output is correct |
11 |
Correct |
54 ms |
7412 KB |
Output is correct |
12 |
Correct |
55 ms |
7468 KB |
Output is correct |
13 |
Correct |
52 ms |
7432 KB |
Output is correct |
14 |
Correct |
52 ms |
7320 KB |
Output is correct |
15 |
Correct |
55 ms |
7468 KB |
Output is correct |
16 |
Correct |
53 ms |
7256 KB |
Output is correct |
17 |
Correct |
51 ms |
7464 KB |
Output is correct |
18 |
Correct |
55 ms |
7272 KB |
Output is correct |
19 |
Correct |
51 ms |
7452 KB |
Output is correct |
20 |
Correct |
54 ms |
7372 KB |
Output is correct |
21 |
Correct |
50 ms |
7244 KB |
Output is correct |
22 |
Correct |
251 ms |
7124 KB |
Output is correct |
23 |
Correct |
247 ms |
7068 KB |
Output is correct |
24 |
Correct |
254 ms |
7036 KB |
Output is correct |
25 |
Correct |
265 ms |
7068 KB |
Output is correct |
26 |
Correct |
249 ms |
7104 KB |
Output is correct |
27 |
Correct |
267 ms |
7016 KB |
Output is correct |
28 |
Correct |
247 ms |
7040 KB |
Output is correct |
29 |
Correct |
239 ms |
7096 KB |
Output is correct |
30 |
Correct |
243 ms |
7020 KB |
Output is correct |
31 |
Correct |
243 ms |
7076 KB |
Output is correct |
32 |
Correct |
248 ms |
7028 KB |
Output is correct |
33 |
Correct |
246 ms |
7132 KB |
Output is correct |
34 |
Correct |
240 ms |
7048 KB |
Output is correct |
35 |
Correct |
239 ms |
7080 KB |
Output is correct |
36 |
Correct |
235 ms |
7012 KB |
Output is correct |
37 |
Correct |
240 ms |
7276 KB |
Output is correct |
38 |
Correct |
254 ms |
7068 KB |
Output is correct |
39 |
Correct |
250 ms |
7084 KB |
Output is correct |
40 |
Correct |
237 ms |
7072 KB |
Output is correct |
41 |
Correct |
260 ms |
7024 KB |
Output is correct |
42 |
Correct |
234 ms |
7124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
6580 KB |
Output is correct |
2 |
Correct |
53 ms |
7276 KB |
Output is correct |
3 |
Correct |
50 ms |
7464 KB |
Output is correct |
4 |
Correct |
51 ms |
7460 KB |
Output is correct |
5 |
Correct |
51 ms |
7260 KB |
Output is correct |
6 |
Correct |
53 ms |
7320 KB |
Output is correct |
7 |
Correct |
54 ms |
7460 KB |
Output is correct |
8 |
Correct |
52 ms |
7276 KB |
Output is correct |
9 |
Correct |
50 ms |
7252 KB |
Output is correct |
10 |
Correct |
55 ms |
7364 KB |
Output is correct |
11 |
Correct |
54 ms |
7412 KB |
Output is correct |
12 |
Correct |
55 ms |
7468 KB |
Output is correct |
13 |
Correct |
52 ms |
7432 KB |
Output is correct |
14 |
Correct |
52 ms |
7320 KB |
Output is correct |
15 |
Correct |
55 ms |
7468 KB |
Output is correct |
16 |
Correct |
53 ms |
7256 KB |
Output is correct |
17 |
Correct |
51 ms |
7464 KB |
Output is correct |
18 |
Correct |
55 ms |
7272 KB |
Output is correct |
19 |
Correct |
51 ms |
7452 KB |
Output is correct |
20 |
Correct |
54 ms |
7372 KB |
Output is correct |
21 |
Correct |
50 ms |
7244 KB |
Output is correct |
22 |
Correct |
251 ms |
7124 KB |
Output is correct |
23 |
Correct |
247 ms |
7068 KB |
Output is correct |
24 |
Correct |
254 ms |
7036 KB |
Output is correct |
25 |
Correct |
265 ms |
7068 KB |
Output is correct |
26 |
Correct |
249 ms |
7104 KB |
Output is correct |
27 |
Correct |
267 ms |
7016 KB |
Output is correct |
28 |
Correct |
247 ms |
7040 KB |
Output is correct |
29 |
Correct |
239 ms |
7096 KB |
Output is correct |
30 |
Correct |
243 ms |
7020 KB |
Output is correct |
31 |
Correct |
243 ms |
7076 KB |
Output is correct |
32 |
Correct |
248 ms |
7028 KB |
Output is correct |
33 |
Correct |
246 ms |
7132 KB |
Output is correct |
34 |
Correct |
240 ms |
7048 KB |
Output is correct |
35 |
Correct |
239 ms |
7080 KB |
Output is correct |
36 |
Correct |
235 ms |
7012 KB |
Output is correct |
37 |
Correct |
240 ms |
7276 KB |
Output is correct |
38 |
Correct |
254 ms |
7068 KB |
Output is correct |
39 |
Correct |
250 ms |
7084 KB |
Output is correct |
40 |
Correct |
237 ms |
7072 KB |
Output is correct |
41 |
Correct |
260 ms |
7024 KB |
Output is correct |
42 |
Correct |
234 ms |
7124 KB |
Output is correct |
43 |
Execution timed out |
1140 ms |
14228 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
6580 KB |
Output is correct |
2 |
Correct |
53 ms |
7276 KB |
Output is correct |
3 |
Correct |
50 ms |
7464 KB |
Output is correct |
4 |
Correct |
51 ms |
7460 KB |
Output is correct |
5 |
Correct |
51 ms |
7260 KB |
Output is correct |
6 |
Correct |
53 ms |
7320 KB |
Output is correct |
7 |
Correct |
54 ms |
7460 KB |
Output is correct |
8 |
Correct |
52 ms |
7276 KB |
Output is correct |
9 |
Correct |
50 ms |
7252 KB |
Output is correct |
10 |
Correct |
55 ms |
7364 KB |
Output is correct |
11 |
Correct |
54 ms |
7412 KB |
Output is correct |
12 |
Correct |
55 ms |
7468 KB |
Output is correct |
13 |
Correct |
52 ms |
7432 KB |
Output is correct |
14 |
Correct |
52 ms |
7320 KB |
Output is correct |
15 |
Correct |
55 ms |
7468 KB |
Output is correct |
16 |
Correct |
53 ms |
7256 KB |
Output is correct |
17 |
Correct |
51 ms |
7464 KB |
Output is correct |
18 |
Correct |
55 ms |
7272 KB |
Output is correct |
19 |
Correct |
51 ms |
7452 KB |
Output is correct |
20 |
Correct |
54 ms |
7372 KB |
Output is correct |
21 |
Correct |
50 ms |
7244 KB |
Output is correct |
22 |
Correct |
251 ms |
7124 KB |
Output is correct |
23 |
Correct |
247 ms |
7068 KB |
Output is correct |
24 |
Correct |
254 ms |
7036 KB |
Output is correct |
25 |
Correct |
265 ms |
7068 KB |
Output is correct |
26 |
Correct |
249 ms |
7104 KB |
Output is correct |
27 |
Correct |
267 ms |
7016 KB |
Output is correct |
28 |
Correct |
247 ms |
7040 KB |
Output is correct |
29 |
Correct |
239 ms |
7096 KB |
Output is correct |
30 |
Correct |
243 ms |
7020 KB |
Output is correct |
31 |
Correct |
243 ms |
7076 KB |
Output is correct |
32 |
Correct |
248 ms |
7028 KB |
Output is correct |
33 |
Correct |
246 ms |
7132 KB |
Output is correct |
34 |
Correct |
240 ms |
7048 KB |
Output is correct |
35 |
Correct |
239 ms |
7080 KB |
Output is correct |
36 |
Correct |
235 ms |
7012 KB |
Output is correct |
37 |
Correct |
240 ms |
7276 KB |
Output is correct |
38 |
Correct |
254 ms |
7068 KB |
Output is correct |
39 |
Correct |
250 ms |
7084 KB |
Output is correct |
40 |
Correct |
237 ms |
7072 KB |
Output is correct |
41 |
Correct |
260 ms |
7024 KB |
Output is correct |
42 |
Correct |
234 ms |
7124 KB |
Output is correct |
43 |
Execution timed out |
1140 ms |
14228 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |