Submission #1026448

# Submission time Handle Problem Language Result Execution time Memory
1026448 2024-07-18T06:09:40 Z caterpillow Cubeword (CEOI19_cubeword) C++17
100 / 100
1086 ms 10956 KB
#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;

const ll mod = 998244353;

#define add(a, b) a += b; a -= (a >= mod) * 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;
int mp[1000];

ll triples[m][m][m]; // first index is the "sink", while next three are the sources
ll solve(vt<vt<ll>>& cnt) {
    memset(triples, 0, sizeof(triples));
    ll ans = 0;
    F0R (i, m) {
        F0R (a, m) {
            F0R (b, m) {
                F0R (c, m) {
                    add(triples[a][b][c], (((cnt[a][i] * cnt[b][i]) % mod) * cnt[c][i]) % mod);
                }
            }
        }
    }
    F0R (a, m) {
        F0R (b, m) {
            F0R (c, m) {
                F0R (d, m) {
                    add(ans, (((((triples[a][b][c] * triples[a][b][d]) % mod) * triples[a][c][d]) % mod) * triples[b][c][d]) % mod);
                }
            }
        }
    }
    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<vt<ll>>> cnts(11, vt<vt<ll>>(m, vt<ll>(m)));
    F0R (i, n) {
        string st; cin >> st;
        string rst = st;
        reverse(all(rst));
        if (pool.count(rst)) continue;
        cnts[size(st)][mp[st[0]]][mp[st.back()]]++;
        if (st != rst) cnts[size(rst)][mp[rst[0]]][mp[rst.back()]]++;
        pool.insert(st);
    }
    ll ans = 0;
    FOR (i, 3, 11) {
        add(ans, solve(cnts[i]));
    }
    cout << ans << endl;
}

Compilation message

cubeword.cpp:71:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   71 | main() {
      | ^~~~
cubeword.cpp: In function 'int main()':
cubeword.cpp:76:19: warning: array subscript has type 'char' [-Wchar-subscripts]
   76 |         mp[alph[i]] = i;
      |                   ^
cubeword.cpp:87:32: warning: array subscript has type 'char' [-Wchar-subscripts]
   87 |         cnts[size(st)][mp[st[0]]][mp[st.back()]]++;
      |                                ^
cubeword.cpp:87:45: warning: array subscript has type 'char' [-Wchar-subscripts]
   87 |         cnts[size(st)][mp[st[0]]][mp[st.back()]]++;
      |                                      ~~~~~~~^~
cubeword.cpp:88:49: warning: array subscript has type 'char' [-Wchar-subscripts]
   88 |         if (st != rst) cnts[size(rst)][mp[rst[0]]][mp[rst.back()]]++;
      |                                                 ^
cubeword.cpp:88:63: warning: array subscript has type 'char' [-Wchar-subscripts]
   88 |         if (st != rst) cnts[size(rst)][mp[rst[0]]][mp[rst.back()]]++;
      |                                                       ~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 1012 ms 9672 KB Output is correct
2 Correct 1016 ms 9668 KB Output is correct
3 Correct 1027 ms 9672 KB Output is correct
4 Correct 968 ms 9672 KB Output is correct
5 Correct 965 ms 9808 KB Output is correct
6 Correct 970 ms 9672 KB Output is correct
7 Correct 923 ms 9672 KB Output is correct
8 Correct 965 ms 9676 KB Output is correct
9 Correct 949 ms 9668 KB Output is correct
10 Correct 983 ms 9668 KB Output is correct
11 Correct 1033 ms 9688 KB Output is correct
12 Correct 1000 ms 9668 KB Output is correct
13 Correct 1022 ms 9668 KB Output is correct
14 Correct 1027 ms 9680 KB Output is correct
15 Correct 999 ms 9680 KB Output is correct
16 Correct 976 ms 9676 KB Output is correct
17 Correct 1030 ms 9668 KB Output is correct
18 Correct 1037 ms 9668 KB Output is correct
19 Correct 1030 ms 9676 KB Output is correct
20 Correct 1026 ms 9668 KB Output is correct
21 Correct 1086 ms 9612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1012 ms 9672 KB Output is correct
2 Correct 1016 ms 9668 KB Output is correct
3 Correct 1027 ms 9672 KB Output is correct
4 Correct 968 ms 9672 KB Output is correct
5 Correct 965 ms 9808 KB Output is correct
6 Correct 970 ms 9672 KB Output is correct
7 Correct 923 ms 9672 KB Output is correct
8 Correct 965 ms 9676 KB Output is correct
9 Correct 949 ms 9668 KB Output is correct
10 Correct 983 ms 9668 KB Output is correct
11 Correct 1033 ms 9688 KB Output is correct
12 Correct 1000 ms 9668 KB Output is correct
13 Correct 1022 ms 9668 KB Output is correct
14 Correct 1027 ms 9680 KB Output is correct
15 Correct 999 ms 9680 KB Output is correct
16 Correct 976 ms 9676 KB Output is correct
17 Correct 1030 ms 9668 KB Output is correct
18 Correct 1037 ms 9668 KB Output is correct
19 Correct 1030 ms 9676 KB Output is correct
20 Correct 1026 ms 9668 KB Output is correct
21 Correct 1086 ms 9612 KB Output is correct
22 Correct 1043 ms 9928 KB Output is correct
23 Correct 1010 ms 9932 KB Output is correct
24 Correct 976 ms 9928 KB Output is correct
25 Correct 965 ms 9928 KB Output is correct
26 Correct 955 ms 9936 KB Output is correct
27 Correct 929 ms 9924 KB Output is correct
28 Correct 964 ms 9924 KB Output is correct
29 Correct 973 ms 9924 KB Output is correct
30 Correct 985 ms 9920 KB Output is correct
31 Correct 972 ms 9932 KB Output is correct
32 Correct 1018 ms 9936 KB Output is correct
33 Correct 1058 ms 9844 KB Output is correct
34 Correct 984 ms 10004 KB Output is correct
35 Correct 1002 ms 9932 KB Output is correct
36 Correct 996 ms 9932 KB Output is correct
37 Correct 1018 ms 9932 KB Output is correct
38 Correct 1049 ms 9928 KB Output is correct
39 Correct 1032 ms 9924 KB Output is correct
40 Correct 1027 ms 9936 KB Output is correct
41 Correct 1038 ms 9924 KB Output is correct
42 Correct 1014 ms 9936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1012 ms 9672 KB Output is correct
2 Correct 1016 ms 9668 KB Output is correct
3 Correct 1027 ms 9672 KB Output is correct
4 Correct 968 ms 9672 KB Output is correct
5 Correct 965 ms 9808 KB Output is correct
6 Correct 970 ms 9672 KB Output is correct
7 Correct 923 ms 9672 KB Output is correct
8 Correct 965 ms 9676 KB Output is correct
9 Correct 949 ms 9668 KB Output is correct
10 Correct 983 ms 9668 KB Output is correct
11 Correct 1033 ms 9688 KB Output is correct
12 Correct 1000 ms 9668 KB Output is correct
13 Correct 1022 ms 9668 KB Output is correct
14 Correct 1027 ms 9680 KB Output is correct
15 Correct 999 ms 9680 KB Output is correct
16 Correct 976 ms 9676 KB Output is correct
17 Correct 1030 ms 9668 KB Output is correct
18 Correct 1037 ms 9668 KB Output is correct
19 Correct 1030 ms 9676 KB Output is correct
20 Correct 1026 ms 9668 KB Output is correct
21 Correct 1086 ms 9612 KB Output is correct
22 Correct 1043 ms 9928 KB Output is correct
23 Correct 1010 ms 9932 KB Output is correct
24 Correct 976 ms 9928 KB Output is correct
25 Correct 965 ms 9928 KB Output is correct
26 Correct 955 ms 9936 KB Output is correct
27 Correct 929 ms 9924 KB Output is correct
28 Correct 964 ms 9924 KB Output is correct
29 Correct 973 ms 9924 KB Output is correct
30 Correct 985 ms 9920 KB Output is correct
31 Correct 972 ms 9932 KB Output is correct
32 Correct 1018 ms 9936 KB Output is correct
33 Correct 1058 ms 9844 KB Output is correct
34 Correct 984 ms 10004 KB Output is correct
35 Correct 1002 ms 9932 KB Output is correct
36 Correct 996 ms 9932 KB Output is correct
37 Correct 1018 ms 9932 KB Output is correct
38 Correct 1049 ms 9928 KB Output is correct
39 Correct 1032 ms 9924 KB Output is correct
40 Correct 1027 ms 9936 KB Output is correct
41 Correct 1038 ms 9924 KB Output is correct
42 Correct 1014 ms 9936 KB Output is correct
43 Correct 976 ms 10188 KB Output is correct
44 Correct 1033 ms 10180 KB Output is correct
45 Correct 1083 ms 10180 KB Output is correct
46 Correct 1029 ms 10188 KB Output is correct
47 Correct 1046 ms 10184 KB Output is correct
48 Correct 1025 ms 10188 KB Output is correct
49 Correct 1037 ms 10032 KB Output is correct
50 Correct 1037 ms 10188 KB Output is correct
51 Correct 1053 ms 10180 KB Output is correct
52 Correct 1046 ms 9928 KB Output is correct
53 Correct 1035 ms 10180 KB Output is correct
54 Correct 1044 ms 10184 KB Output is correct
55 Correct 1035 ms 10188 KB Output is correct
56 Correct 1018 ms 9928 KB Output is correct
57 Correct 1022 ms 10192 KB Output is correct
58 Correct 980 ms 10040 KB Output is correct
59 Correct 1018 ms 10188 KB Output is correct
60 Correct 1040 ms 10192 KB Output is correct
61 Correct 1006 ms 10184 KB Output is correct
62 Correct 1036 ms 10180 KB Output is correct
63 Correct 1033 ms 9956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1012 ms 9672 KB Output is correct
2 Correct 1016 ms 9668 KB Output is correct
3 Correct 1027 ms 9672 KB Output is correct
4 Correct 968 ms 9672 KB Output is correct
5 Correct 965 ms 9808 KB Output is correct
6 Correct 970 ms 9672 KB Output is correct
7 Correct 923 ms 9672 KB Output is correct
8 Correct 965 ms 9676 KB Output is correct
9 Correct 949 ms 9668 KB Output is correct
10 Correct 983 ms 9668 KB Output is correct
11 Correct 1033 ms 9688 KB Output is correct
12 Correct 1000 ms 9668 KB Output is correct
13 Correct 1022 ms 9668 KB Output is correct
14 Correct 1027 ms 9680 KB Output is correct
15 Correct 999 ms 9680 KB Output is correct
16 Correct 976 ms 9676 KB Output is correct
17 Correct 1030 ms 9668 KB Output is correct
18 Correct 1037 ms 9668 KB Output is correct
19 Correct 1030 ms 9676 KB Output is correct
20 Correct 1026 ms 9668 KB Output is correct
21 Correct 1086 ms 9612 KB Output is correct
22 Correct 1043 ms 9928 KB Output is correct
23 Correct 1010 ms 9932 KB Output is correct
24 Correct 976 ms 9928 KB Output is correct
25 Correct 965 ms 9928 KB Output is correct
26 Correct 955 ms 9936 KB Output is correct
27 Correct 929 ms 9924 KB Output is correct
28 Correct 964 ms 9924 KB Output is correct
29 Correct 973 ms 9924 KB Output is correct
30 Correct 985 ms 9920 KB Output is correct
31 Correct 972 ms 9932 KB Output is correct
32 Correct 1018 ms 9936 KB Output is correct
33 Correct 1058 ms 9844 KB Output is correct
34 Correct 984 ms 10004 KB Output is correct
35 Correct 1002 ms 9932 KB Output is correct
36 Correct 996 ms 9932 KB Output is correct
37 Correct 1018 ms 9932 KB Output is correct
38 Correct 1049 ms 9928 KB Output is correct
39 Correct 1032 ms 9924 KB Output is correct
40 Correct 1027 ms 9936 KB Output is correct
41 Correct 1038 ms 9924 KB Output is correct
42 Correct 1014 ms 9936 KB Output is correct
43 Correct 976 ms 10188 KB Output is correct
44 Correct 1033 ms 10180 KB Output is correct
45 Correct 1083 ms 10180 KB Output is correct
46 Correct 1029 ms 10188 KB Output is correct
47 Correct 1046 ms 10184 KB Output is correct
48 Correct 1025 ms 10188 KB Output is correct
49 Correct 1037 ms 10032 KB Output is correct
50 Correct 1037 ms 10188 KB Output is correct
51 Correct 1053 ms 10180 KB Output is correct
52 Correct 1046 ms 9928 KB Output is correct
53 Correct 1035 ms 10180 KB Output is correct
54 Correct 1044 ms 10184 KB Output is correct
55 Correct 1035 ms 10188 KB Output is correct
56 Correct 1018 ms 9928 KB Output is correct
57 Correct 1022 ms 10192 KB Output is correct
58 Correct 980 ms 10040 KB Output is correct
59 Correct 1018 ms 10188 KB Output is correct
60 Correct 1040 ms 10192 KB Output is correct
61 Correct 1006 ms 10184 KB Output is correct
62 Correct 1036 ms 10180 KB Output is correct
63 Correct 1033 ms 9956 KB Output is correct
64 Correct 1052 ms 10220 KB Output is correct
65 Correct 1037 ms 10204 KB Output is correct
66 Correct 1022 ms 10188 KB Output is correct
67 Correct 1034 ms 10092 KB Output is correct
68 Correct 1010 ms 10180 KB Output is correct
69 Correct 1030 ms 10436 KB Output is correct
70 Correct 988 ms 10180 KB Output is correct
71 Correct 969 ms 10192 KB Output is correct
72 Correct 905 ms 10184 KB Output is correct
73 Correct 971 ms 10188 KB Output is correct
74 Correct 938 ms 10184 KB Output is correct
75 Correct 974 ms 10228 KB Output is correct
76 Correct 950 ms 10180 KB Output is correct
77 Correct 957 ms 10180 KB Output is correct
78 Correct 991 ms 10180 KB Output is correct
79 Correct 966 ms 10188 KB Output is correct
80 Correct 943 ms 10180 KB Output is correct
81 Correct 989 ms 10180 KB Output is correct
82 Correct 957 ms 10184 KB Output is correct
83 Correct 962 ms 10184 KB Output is correct
84 Correct 983 ms 10956 KB Output is correct