#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using db = long double;
using ll = long long;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
#define vt vector
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()
#define size(x) ((int) (x).size())
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0R(i, b) FOR (i, 0, b)
#define endl '\n'
const ll INF = 1e18;
const int inf = 1e9;
template<typename Tuple, size_t... Is>
void print_tuple(const Tuple& t, index_sequence<Is...>) {
((cerr << (Is == 0 ? "{" : ", ") << get<Is>(t)), ...);
cerr << "}";
}
template<typename... Args>
ostream& operator<<(ostream& os, const tuple<Args...>& t) {
print_tuple(t, index_sequence_for<Args...>{});
return os;
}
ostream& operator<<(ostream& os, string& s) {
for (char c : s) os << c;
return os;
}
template<template<typename> class Container, typename T>
ostream& operator<<(ostream& os, Container<T> o) {
os << "{";
int g = size(o);
for (auto i : o) os << i << ((--g) == 0 ? "" : ", ");
os << "}";
return os;
}
template<typename T, typename V>
ostream& operator<<(ostream& os, const pair<T, V> p) {
os << "{" << p.f << ", " << p.s << "}";
return os;
}
template <typename T, typename... V>
void printer(const char *names, T &&head, V &&...tail) {
int i = 0;
while (names[i] != '\0' && names[i] != ',') i++;
constexpr bool is_str = is_same_v<decay_t<T>, const char*>;
if constexpr (is_str) cerr << head; // ignore directly passed strings
else cerr.write(names, i) << " = " << head;
if constexpr (sizeof...(tail)) cerr << (is_str ? "" : ","), printer(names + i + 1, tail...);
else cerr << endl;
}
#ifdef LOCAL
#define dbg(...) std::cerr << __LINE__ << ": ", printer(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(x...)
#define cerr if (0) std::cerr
#endif
const ll mod = 998244353;
void add(ll& a, ll b) {
a = (a + b) % mod;
}
template<typename T>
T mul(T t) {
return t;
}
template<typename T, typename... Args>
T mul(T f, Args... args) {
return (f * mul(args...)) % mod;
}
ll cube(ll x) {
return (((x * x) % mod) * x) % mod;
}
/*
bucket by word lengths
only care about first and last characters
add both forwards and backwards versions
remove words that are the reverse of each other
dont add duplicates for palindromes
precompute the # of ways to colour a cell if its neighbours are values [a][b][c]
*/
const string alph = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const ll m = 62;
map<char, int> mp;
ll cnt[m][m]; // i -> j
ll triples[m][m][m]; // first index is the "sink", while next three are the sources
ll solve(vt<pi>& pairs) {
memset(cnt, 0, sizeof(cnt));
memset(triples, 0, sizeof(triples));
for (auto [i, j] : pairs) cnt[i][j]++;
ll ans = 0;
F0R (i, m) {
F0R (a, m) {
if (!cnt[a][i]) continue;
F0R (b, m) {
if (!cnt[b][i]) continue;
F0R (c, m) {
if (!cnt[c][i]) continue;
add(triples[a][b][c], mul(cnt[a][i], cnt[b][i], cnt[c][i]));
}
}
}
}
F0R (a, m) {
F0R (b, m) {
F0R (c, m) {
F0R (d, m) {
add(ans, mul(triples[a][b][c], triples[a][b][d], triples[a][c][d], triples[b][c][d]));
}
}
}
}
return ans;
}
main() {
cin.tie(0)->sync_with_stdio(0);
assert(size(alph) == m);
F0R (i, size(alph)) {
mp[alph[i]] = i;
}
int n; cin >> n;
unordered_set<string> pool;
vt<vt<pi>> lengths(11);
F0R (i, n) {
string st; cin >> st;
string rst = st;
reverse(all(rst));
if (pool.count(rst)) continue;
lengths[size(st)].pb({mp[st[0]], mp[st.back()]});
if (st != rst) lengths[size(rst)].pb({mp[rst[0]], mp[rst.back()]});
pool.insert(st);
}
ll ans = 0;
FOR (i, 3, 11) {
add(ans, solve(lengths[i]));
}
cout << ans << endl;
}
Compilation message
cubeword.cpp:143:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
143 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
765 ms |
11860 KB |
Output is correct |
2 |
Correct |
755 ms |
11716 KB |
Output is correct |
3 |
Correct |
743 ms |
11712 KB |
Output is correct |
4 |
Correct |
797 ms |
11712 KB |
Output is correct |
5 |
Correct |
728 ms |
11708 KB |
Output is correct |
6 |
Correct |
783 ms |
11712 KB |
Output is correct |
7 |
Correct |
775 ms |
11708 KB |
Output is correct |
8 |
Correct |
808 ms |
11720 KB |
Output is correct |
9 |
Correct |
801 ms |
11708 KB |
Output is correct |
10 |
Correct |
767 ms |
11616 KB |
Output is correct |
11 |
Correct |
731 ms |
11844 KB |
Output is correct |
12 |
Correct |
737 ms |
11720 KB |
Output is correct |
13 |
Correct |
747 ms |
11988 KB |
Output is correct |
14 |
Correct |
745 ms |
11712 KB |
Output is correct |
15 |
Correct |
786 ms |
11852 KB |
Output is correct |
16 |
Correct |
774 ms |
11712 KB |
Output is correct |
17 |
Correct |
783 ms |
11852 KB |
Output is correct |
18 |
Correct |
799 ms |
11712 KB |
Output is correct |
19 |
Correct |
814 ms |
11856 KB |
Output is correct |
20 |
Correct |
810 ms |
11852 KB |
Output is correct |
21 |
Correct |
810 ms |
11852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
765 ms |
11860 KB |
Output is correct |
2 |
Correct |
755 ms |
11716 KB |
Output is correct |
3 |
Correct |
743 ms |
11712 KB |
Output is correct |
4 |
Correct |
797 ms |
11712 KB |
Output is correct |
5 |
Correct |
728 ms |
11708 KB |
Output is correct |
6 |
Correct |
783 ms |
11712 KB |
Output is correct |
7 |
Correct |
775 ms |
11708 KB |
Output is correct |
8 |
Correct |
808 ms |
11720 KB |
Output is correct |
9 |
Correct |
801 ms |
11708 KB |
Output is correct |
10 |
Correct |
767 ms |
11616 KB |
Output is correct |
11 |
Correct |
731 ms |
11844 KB |
Output is correct |
12 |
Correct |
737 ms |
11720 KB |
Output is correct |
13 |
Correct |
747 ms |
11988 KB |
Output is correct |
14 |
Correct |
745 ms |
11712 KB |
Output is correct |
15 |
Correct |
786 ms |
11852 KB |
Output is correct |
16 |
Correct |
774 ms |
11712 KB |
Output is correct |
17 |
Correct |
783 ms |
11852 KB |
Output is correct |
18 |
Correct |
799 ms |
11712 KB |
Output is correct |
19 |
Correct |
814 ms |
11856 KB |
Output is correct |
20 |
Correct |
810 ms |
11852 KB |
Output is correct |
21 |
Correct |
810 ms |
11852 KB |
Output is correct |
22 |
Correct |
843 ms |
11976 KB |
Output is correct |
23 |
Correct |
891 ms |
11976 KB |
Output is correct |
24 |
Correct |
847 ms |
11976 KB |
Output is correct |
25 |
Correct |
853 ms |
11976 KB |
Output is correct |
26 |
Correct |
840 ms |
11908 KB |
Output is correct |
27 |
Correct |
828 ms |
11976 KB |
Output is correct |
28 |
Correct |
774 ms |
11976 KB |
Output is correct |
29 |
Correct |
783 ms |
11912 KB |
Output is correct |
30 |
Correct |
777 ms |
11976 KB |
Output is correct |
31 |
Correct |
795 ms |
11976 KB |
Output is correct |
32 |
Correct |
766 ms |
11972 KB |
Output is correct |
33 |
Correct |
784 ms |
12100 KB |
Output is correct |
34 |
Correct |
762 ms |
11972 KB |
Output is correct |
35 |
Correct |
751 ms |
11976 KB |
Output is correct |
36 |
Correct |
757 ms |
11976 KB |
Output is correct |
37 |
Correct |
709 ms |
11976 KB |
Output is correct |
38 |
Correct |
696 ms |
11976 KB |
Output is correct |
39 |
Correct |
756 ms |
11976 KB |
Output is correct |
40 |
Correct |
716 ms |
12060 KB |
Output is correct |
41 |
Correct |
740 ms |
11976 KB |
Output is correct |
42 |
Correct |
713 ms |
11976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
765 ms |
11860 KB |
Output is correct |
2 |
Correct |
755 ms |
11716 KB |
Output is correct |
3 |
Correct |
743 ms |
11712 KB |
Output is correct |
4 |
Correct |
797 ms |
11712 KB |
Output is correct |
5 |
Correct |
728 ms |
11708 KB |
Output is correct |
6 |
Correct |
783 ms |
11712 KB |
Output is correct |
7 |
Correct |
775 ms |
11708 KB |
Output is correct |
8 |
Correct |
808 ms |
11720 KB |
Output is correct |
9 |
Correct |
801 ms |
11708 KB |
Output is correct |
10 |
Correct |
767 ms |
11616 KB |
Output is correct |
11 |
Correct |
731 ms |
11844 KB |
Output is correct |
12 |
Correct |
737 ms |
11720 KB |
Output is correct |
13 |
Correct |
747 ms |
11988 KB |
Output is correct |
14 |
Correct |
745 ms |
11712 KB |
Output is correct |
15 |
Correct |
786 ms |
11852 KB |
Output is correct |
16 |
Correct |
774 ms |
11712 KB |
Output is correct |
17 |
Correct |
783 ms |
11852 KB |
Output is correct |
18 |
Correct |
799 ms |
11712 KB |
Output is correct |
19 |
Correct |
814 ms |
11856 KB |
Output is correct |
20 |
Correct |
810 ms |
11852 KB |
Output is correct |
21 |
Correct |
810 ms |
11852 KB |
Output is correct |
22 |
Correct |
843 ms |
11976 KB |
Output is correct |
23 |
Correct |
891 ms |
11976 KB |
Output is correct |
24 |
Correct |
847 ms |
11976 KB |
Output is correct |
25 |
Correct |
853 ms |
11976 KB |
Output is correct |
26 |
Correct |
840 ms |
11908 KB |
Output is correct |
27 |
Correct |
828 ms |
11976 KB |
Output is correct |
28 |
Correct |
774 ms |
11976 KB |
Output is correct |
29 |
Correct |
783 ms |
11912 KB |
Output is correct |
30 |
Correct |
777 ms |
11976 KB |
Output is correct |
31 |
Correct |
795 ms |
11976 KB |
Output is correct |
32 |
Correct |
766 ms |
11972 KB |
Output is correct |
33 |
Correct |
784 ms |
12100 KB |
Output is correct |
34 |
Correct |
762 ms |
11972 KB |
Output is correct |
35 |
Correct |
751 ms |
11976 KB |
Output is correct |
36 |
Correct |
757 ms |
11976 KB |
Output is correct |
37 |
Correct |
709 ms |
11976 KB |
Output is correct |
38 |
Correct |
696 ms |
11976 KB |
Output is correct |
39 |
Correct |
756 ms |
11976 KB |
Output is correct |
40 |
Correct |
716 ms |
12060 KB |
Output is correct |
41 |
Correct |
740 ms |
11976 KB |
Output is correct |
42 |
Correct |
713 ms |
11976 KB |
Output is correct |
43 |
Correct |
775 ms |
12236 KB |
Output is correct |
44 |
Correct |
735 ms |
12232 KB |
Output is correct |
45 |
Correct |
796 ms |
12228 KB |
Output is correct |
46 |
Correct |
811 ms |
11980 KB |
Output is correct |
47 |
Correct |
787 ms |
12232 KB |
Output is correct |
48 |
Correct |
794 ms |
11984 KB |
Output is correct |
49 |
Correct |
819 ms |
12232 KB |
Output is correct |
50 |
Correct |
774 ms |
12040 KB |
Output is correct |
51 |
Correct |
749 ms |
12232 KB |
Output is correct |
52 |
Correct |
789 ms |
12232 KB |
Output is correct |
53 |
Correct |
814 ms |
12232 KB |
Output is correct |
54 |
Correct |
806 ms |
12232 KB |
Output is correct |
55 |
Correct |
820 ms |
12232 KB |
Output is correct |
56 |
Correct |
821 ms |
12240 KB |
Output is correct |
57 |
Correct |
839 ms |
12036 KB |
Output is correct |
58 |
Correct |
797 ms |
11976 KB |
Output is correct |
59 |
Correct |
814 ms |
12104 KB |
Output is correct |
60 |
Correct |
797 ms |
12120 KB |
Output is correct |
61 |
Correct |
775 ms |
12228 KB |
Output is correct |
62 |
Correct |
809 ms |
11976 KB |
Output is correct |
63 |
Correct |
797 ms |
12240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
765 ms |
11860 KB |
Output is correct |
2 |
Correct |
755 ms |
11716 KB |
Output is correct |
3 |
Correct |
743 ms |
11712 KB |
Output is correct |
4 |
Correct |
797 ms |
11712 KB |
Output is correct |
5 |
Correct |
728 ms |
11708 KB |
Output is correct |
6 |
Correct |
783 ms |
11712 KB |
Output is correct |
7 |
Correct |
775 ms |
11708 KB |
Output is correct |
8 |
Correct |
808 ms |
11720 KB |
Output is correct |
9 |
Correct |
801 ms |
11708 KB |
Output is correct |
10 |
Correct |
767 ms |
11616 KB |
Output is correct |
11 |
Correct |
731 ms |
11844 KB |
Output is correct |
12 |
Correct |
737 ms |
11720 KB |
Output is correct |
13 |
Correct |
747 ms |
11988 KB |
Output is correct |
14 |
Correct |
745 ms |
11712 KB |
Output is correct |
15 |
Correct |
786 ms |
11852 KB |
Output is correct |
16 |
Correct |
774 ms |
11712 KB |
Output is correct |
17 |
Correct |
783 ms |
11852 KB |
Output is correct |
18 |
Correct |
799 ms |
11712 KB |
Output is correct |
19 |
Correct |
814 ms |
11856 KB |
Output is correct |
20 |
Correct |
810 ms |
11852 KB |
Output is correct |
21 |
Correct |
810 ms |
11852 KB |
Output is correct |
22 |
Correct |
843 ms |
11976 KB |
Output is correct |
23 |
Correct |
891 ms |
11976 KB |
Output is correct |
24 |
Correct |
847 ms |
11976 KB |
Output is correct |
25 |
Correct |
853 ms |
11976 KB |
Output is correct |
26 |
Correct |
840 ms |
11908 KB |
Output is correct |
27 |
Correct |
828 ms |
11976 KB |
Output is correct |
28 |
Correct |
774 ms |
11976 KB |
Output is correct |
29 |
Correct |
783 ms |
11912 KB |
Output is correct |
30 |
Correct |
777 ms |
11976 KB |
Output is correct |
31 |
Correct |
795 ms |
11976 KB |
Output is correct |
32 |
Correct |
766 ms |
11972 KB |
Output is correct |
33 |
Correct |
784 ms |
12100 KB |
Output is correct |
34 |
Correct |
762 ms |
11972 KB |
Output is correct |
35 |
Correct |
751 ms |
11976 KB |
Output is correct |
36 |
Correct |
757 ms |
11976 KB |
Output is correct |
37 |
Correct |
709 ms |
11976 KB |
Output is correct |
38 |
Correct |
696 ms |
11976 KB |
Output is correct |
39 |
Correct |
756 ms |
11976 KB |
Output is correct |
40 |
Correct |
716 ms |
12060 KB |
Output is correct |
41 |
Correct |
740 ms |
11976 KB |
Output is correct |
42 |
Correct |
713 ms |
11976 KB |
Output is correct |
43 |
Correct |
775 ms |
12236 KB |
Output is correct |
44 |
Correct |
735 ms |
12232 KB |
Output is correct |
45 |
Correct |
796 ms |
12228 KB |
Output is correct |
46 |
Correct |
811 ms |
11980 KB |
Output is correct |
47 |
Correct |
787 ms |
12232 KB |
Output is correct |
48 |
Correct |
794 ms |
11984 KB |
Output is correct |
49 |
Correct |
819 ms |
12232 KB |
Output is correct |
50 |
Correct |
774 ms |
12040 KB |
Output is correct |
51 |
Correct |
749 ms |
12232 KB |
Output is correct |
52 |
Correct |
789 ms |
12232 KB |
Output is correct |
53 |
Correct |
814 ms |
12232 KB |
Output is correct |
54 |
Correct |
806 ms |
12232 KB |
Output is correct |
55 |
Correct |
820 ms |
12232 KB |
Output is correct |
56 |
Correct |
821 ms |
12240 KB |
Output is correct |
57 |
Correct |
839 ms |
12036 KB |
Output is correct |
58 |
Correct |
797 ms |
11976 KB |
Output is correct |
59 |
Correct |
814 ms |
12104 KB |
Output is correct |
60 |
Correct |
797 ms |
12120 KB |
Output is correct |
61 |
Correct |
775 ms |
12228 KB |
Output is correct |
62 |
Correct |
809 ms |
11976 KB |
Output is correct |
63 |
Correct |
797 ms |
12240 KB |
Output is correct |
64 |
Execution timed out |
1162 ms |
12232 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |