Submission #1041164

# Submission time Handle Problem Language Result Execution time Memory
1041164 2024-08-01T16:25:13 Z anton Cubeword (CEOI19_cubeword) C++17
84 / 100
211 ms 16976 KB
#include<bits/stdc++.h>

using namespace std;
#define int long long
#define pii pair<int, int>

const int MOD = 998244353LL;
const int MAX_N =32LL;
int nbP[MAX_N][MAX_N];
int D[MAX_N][MAX_N][MAX_N];
int RES = 0;


bool is_palindrome(string s){
    bool ok = true;
    for(int i = 0; i<(s.size()+1)/2; i++){
        ok &= (s[i] == s[s.size()-i-1]);
    }
    return ok;
}

int mexp(int a, int b){
    int res = 1;
    vector<int>pows(1, a);


    for(int i= 1; i<35; i++){
        pows.push_back((pows.back()*pows.back())%MOD);
    }
    
    for(int i = 0; i<35; i++){
        if(0LL!=((1LL<<i)&b)){
            res = (res * pows[i])%MOD;
        }
    }

    return res;
}

unordered_map<int, int> inv;
int modinv(int a){
    if(inv.find(a)!=inv.end()){
        return inv[a];
    }
    int b= mexp(a, MOD-2);
    inv[a] = b;
    assert((a*b)%MOD == 1);
    return b;
}

map<char, int> assign;

void construct_table(){
    for(char c=  'a'; c<='p'; c++){
        assign[c] = assign.size();
    }
    for(char c=  'A'; c<='P'; c++){
        assign[c] = assign.size();
    }
    /*for(char c=  '0'; c<='9'; c++){
        assign[c] = assign.size();
    }*/
}
vector<int> transform(string s){

    vector<int> res(s.size());
    for(int i = 0; i<s.size(); i++){
        res[i] = assign[s[i]];
    }
    return res;
}

int fact(int u){
    if(u == 0){
        return 1;
    }
    return fact(u-1)*u;
}

int nbperm(vector<int> v){
    vector<pii> compressed;

    for(auto e: v){
        if(compressed.size() == 0 || e != compressed.back().first){
            compressed.push_back({e, 1});
        }
        else{
            compressed.back().second++;
        }
    }

    int res= fact(v.size());

    for(auto e: compressed){
        res = (res/fact(e.second));
    }
    return res;
}
int prod(vector<int> v){
    int res= 1;
    for(auto e: v){
        res= (res*e)%MOD;
    }
    return res;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N;
    cin>>N;
    vector<set<string>> words(11);
    construct_table();
    for(int i = 0; i<N; i++){
        string s;
        cin>>s;
        if(is_palindrome(s)){
            words[s.size()].insert(s);
        }
        else{
            words[s.size()].insert(s);
            reverse(s.begin(), s.end());
            words[s.size()].insert(s);
        }
    }

    //vector<pair<int, vector<int>>> colorings=  iterate_all(8,6);
    for(int len = 3; len<=10; len++){
        fill(&nbP[0][0], &nbP[MAX_N][0], 0);
        fill(&D[0][0][0], &D[MAX_N][0][0], 0);


        for(auto e: words[len]){
            auto v = transform(e);
            int a=  v[0];
            int b= v.back();
            //cout<<e<<" "<<a<<" "<<b<<endl;
            nbP[a][b]++;
        }

        for(int i = 0; i<MAX_N; i++){
            for(int j = i; j<MAX_N; j++){
                for(int k = j; k<MAX_N; k++){
                    for(int l = 0; l<MAX_N; l++){
                        D[i][j][k] = (D[i][j][k]+prod({nbP[l][i],nbP[l][j], nbP[l][k]}))%MOD;
                        
                    }
                }
            }
        }


        for(int i = 0; i<MAX_N; i++){
            for(int j = i; j<MAX_N; j++){
                for(int k = j; k<MAX_N; k++){
                    for(int h = k; h<MAX_N; h++){
                        int coef = nbperm({i, j, k, h});
                        RES =(RES+ prod({coef, D[i][j][k], D[j][k][h], D[i][k][h], D[i][j][h]}))%MOD;
                    }
                }
            }
        }
    }

    cout<<RES<<endl;
}

Compilation message

cubeword.cpp: In function 'bool is_palindrome(std::string)':
cubeword.cpp:16:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   16 |     for(int i = 0; i<(s.size()+1)/2; i++){
      |                    ~^~~~~~~~~~~~~~~
cubeword.cpp: In function 'std::vector<long long int> transform(std::string)':
cubeword.cpp:67:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |     for(int i = 0; i<s.size(); i++){
      |                    ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 163 ms 15188 KB Output is correct
2 Correct 170 ms 15808 KB Output is correct
3 Correct 185 ms 15956 KB Output is correct
4 Correct 156 ms 15952 KB Output is correct
5 Correct 168 ms 15952 KB Output is correct
6 Correct 178 ms 15956 KB Output is correct
7 Correct 180 ms 15952 KB Output is correct
8 Correct 162 ms 16056 KB Output is correct
9 Correct 171 ms 16208 KB Output is correct
10 Correct 162 ms 15832 KB Output is correct
11 Correct 167 ms 15952 KB Output is correct
12 Correct 157 ms 15888 KB Output is correct
13 Correct 164 ms 15952 KB Output is correct
14 Correct 204 ms 15956 KB Output is correct
15 Correct 181 ms 15952 KB Output is correct
16 Correct 160 ms 15952 KB Output is correct
17 Correct 167 ms 16052 KB Output is correct
18 Correct 160 ms 15840 KB Output is correct
19 Correct 176 ms 16208 KB Output is correct
20 Correct 166 ms 15956 KB Output is correct
21 Correct 191 ms 15948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 163 ms 15188 KB Output is correct
2 Correct 170 ms 15808 KB Output is correct
3 Correct 185 ms 15956 KB Output is correct
4 Correct 156 ms 15952 KB Output is correct
5 Correct 168 ms 15952 KB Output is correct
6 Correct 178 ms 15956 KB Output is correct
7 Correct 180 ms 15952 KB Output is correct
8 Correct 162 ms 16056 KB Output is correct
9 Correct 171 ms 16208 KB Output is correct
10 Correct 162 ms 15832 KB Output is correct
11 Correct 167 ms 15952 KB Output is correct
12 Correct 157 ms 15888 KB Output is correct
13 Correct 164 ms 15952 KB Output is correct
14 Correct 204 ms 15956 KB Output is correct
15 Correct 181 ms 15952 KB Output is correct
16 Correct 160 ms 15952 KB Output is correct
17 Correct 167 ms 16052 KB Output is correct
18 Correct 160 ms 15840 KB Output is correct
19 Correct 176 ms 16208 KB Output is correct
20 Correct 166 ms 15956 KB Output is correct
21 Correct 191 ms 15948 KB Output is correct
22 Correct 163 ms 16404 KB Output is correct
23 Correct 167 ms 16468 KB Output is correct
24 Correct 167 ms 16468 KB Output is correct
25 Correct 179 ms 16468 KB Output is correct
26 Correct 183 ms 16652 KB Output is correct
27 Correct 168 ms 16464 KB Output is correct
28 Correct 170 ms 16524 KB Output is correct
29 Correct 165 ms 16412 KB Output is correct
30 Correct 176 ms 16464 KB Output is correct
31 Correct 170 ms 16444 KB Output is correct
32 Correct 161 ms 16468 KB Output is correct
33 Correct 171 ms 16464 KB Output is correct
34 Correct 174 ms 16464 KB Output is correct
35 Correct 168 ms 16468 KB Output is correct
36 Correct 190 ms 16468 KB Output is correct
37 Correct 195 ms 16528 KB Output is correct
38 Correct 167 ms 16516 KB Output is correct
39 Correct 178 ms 16384 KB Output is correct
40 Correct 173 ms 16376 KB Output is correct
41 Correct 168 ms 16492 KB Output is correct
42 Correct 173 ms 16452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 163 ms 15188 KB Output is correct
2 Correct 170 ms 15808 KB Output is correct
3 Correct 185 ms 15956 KB Output is correct
4 Correct 156 ms 15952 KB Output is correct
5 Correct 168 ms 15952 KB Output is correct
6 Correct 178 ms 15956 KB Output is correct
7 Correct 180 ms 15952 KB Output is correct
8 Correct 162 ms 16056 KB Output is correct
9 Correct 171 ms 16208 KB Output is correct
10 Correct 162 ms 15832 KB Output is correct
11 Correct 167 ms 15952 KB Output is correct
12 Correct 157 ms 15888 KB Output is correct
13 Correct 164 ms 15952 KB Output is correct
14 Correct 204 ms 15956 KB Output is correct
15 Correct 181 ms 15952 KB Output is correct
16 Correct 160 ms 15952 KB Output is correct
17 Correct 167 ms 16052 KB Output is correct
18 Correct 160 ms 15840 KB Output is correct
19 Correct 176 ms 16208 KB Output is correct
20 Correct 166 ms 15956 KB Output is correct
21 Correct 191 ms 15948 KB Output is correct
22 Correct 163 ms 16404 KB Output is correct
23 Correct 167 ms 16468 KB Output is correct
24 Correct 167 ms 16468 KB Output is correct
25 Correct 179 ms 16468 KB Output is correct
26 Correct 183 ms 16652 KB Output is correct
27 Correct 168 ms 16464 KB Output is correct
28 Correct 170 ms 16524 KB Output is correct
29 Correct 165 ms 16412 KB Output is correct
30 Correct 176 ms 16464 KB Output is correct
31 Correct 170 ms 16444 KB Output is correct
32 Correct 161 ms 16468 KB Output is correct
33 Correct 171 ms 16464 KB Output is correct
34 Correct 174 ms 16464 KB Output is correct
35 Correct 168 ms 16468 KB Output is correct
36 Correct 190 ms 16468 KB Output is correct
37 Correct 195 ms 16528 KB Output is correct
38 Correct 167 ms 16516 KB Output is correct
39 Correct 178 ms 16384 KB Output is correct
40 Correct 173 ms 16376 KB Output is correct
41 Correct 168 ms 16492 KB Output is correct
42 Correct 173 ms 16452 KB Output is correct
43 Correct 174 ms 16764 KB Output is correct
44 Correct 168 ms 16780 KB Output is correct
45 Correct 167 ms 16720 KB Output is correct
46 Correct 176 ms 16800 KB Output is correct
47 Correct 180 ms 16728 KB Output is correct
48 Correct 180 ms 16720 KB Output is correct
49 Correct 183 ms 16628 KB Output is correct
50 Correct 170 ms 16712 KB Output is correct
51 Correct 169 ms 16612 KB Output is correct
52 Correct 173 ms 16568 KB Output is correct
53 Correct 211 ms 16704 KB Output is correct
54 Correct 171 ms 16784 KB Output is correct
55 Correct 176 ms 16652 KB Output is correct
56 Correct 184 ms 16724 KB Output is correct
57 Correct 176 ms 16724 KB Output is correct
58 Correct 172 ms 16720 KB Output is correct
59 Correct 185 ms 16724 KB Output is correct
60 Correct 173 ms 16720 KB Output is correct
61 Correct 170 ms 16716 KB Output is correct
62 Correct 171 ms 16720 KB Output is correct
63 Correct 174 ms 16724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 163 ms 15188 KB Output is correct
2 Correct 170 ms 15808 KB Output is correct
3 Correct 185 ms 15956 KB Output is correct
4 Correct 156 ms 15952 KB Output is correct
5 Correct 168 ms 15952 KB Output is correct
6 Correct 178 ms 15956 KB Output is correct
7 Correct 180 ms 15952 KB Output is correct
8 Correct 162 ms 16056 KB Output is correct
9 Correct 171 ms 16208 KB Output is correct
10 Correct 162 ms 15832 KB Output is correct
11 Correct 167 ms 15952 KB Output is correct
12 Correct 157 ms 15888 KB Output is correct
13 Correct 164 ms 15952 KB Output is correct
14 Correct 204 ms 15956 KB Output is correct
15 Correct 181 ms 15952 KB Output is correct
16 Correct 160 ms 15952 KB Output is correct
17 Correct 167 ms 16052 KB Output is correct
18 Correct 160 ms 15840 KB Output is correct
19 Correct 176 ms 16208 KB Output is correct
20 Correct 166 ms 15956 KB Output is correct
21 Correct 191 ms 15948 KB Output is correct
22 Correct 163 ms 16404 KB Output is correct
23 Correct 167 ms 16468 KB Output is correct
24 Correct 167 ms 16468 KB Output is correct
25 Correct 179 ms 16468 KB Output is correct
26 Correct 183 ms 16652 KB Output is correct
27 Correct 168 ms 16464 KB Output is correct
28 Correct 170 ms 16524 KB Output is correct
29 Correct 165 ms 16412 KB Output is correct
30 Correct 176 ms 16464 KB Output is correct
31 Correct 170 ms 16444 KB Output is correct
32 Correct 161 ms 16468 KB Output is correct
33 Correct 171 ms 16464 KB Output is correct
34 Correct 174 ms 16464 KB Output is correct
35 Correct 168 ms 16468 KB Output is correct
36 Correct 190 ms 16468 KB Output is correct
37 Correct 195 ms 16528 KB Output is correct
38 Correct 167 ms 16516 KB Output is correct
39 Correct 178 ms 16384 KB Output is correct
40 Correct 173 ms 16376 KB Output is correct
41 Correct 168 ms 16492 KB Output is correct
42 Correct 173 ms 16452 KB Output is correct
43 Correct 174 ms 16764 KB Output is correct
44 Correct 168 ms 16780 KB Output is correct
45 Correct 167 ms 16720 KB Output is correct
46 Correct 176 ms 16800 KB Output is correct
47 Correct 180 ms 16728 KB Output is correct
48 Correct 180 ms 16720 KB Output is correct
49 Correct 183 ms 16628 KB Output is correct
50 Correct 170 ms 16712 KB Output is correct
51 Correct 169 ms 16612 KB Output is correct
52 Correct 173 ms 16568 KB Output is correct
53 Correct 211 ms 16704 KB Output is correct
54 Correct 171 ms 16784 KB Output is correct
55 Correct 176 ms 16652 KB Output is correct
56 Correct 184 ms 16724 KB Output is correct
57 Correct 176 ms 16724 KB Output is correct
58 Correct 172 ms 16720 KB Output is correct
59 Correct 185 ms 16724 KB Output is correct
60 Correct 173 ms 16720 KB Output is correct
61 Correct 170 ms 16716 KB Output is correct
62 Correct 171 ms 16720 KB Output is correct
63 Correct 174 ms 16724 KB Output is correct
64 Incorrect 174 ms 16976 KB Output isn't correct
65 Halted 0 ms 0 KB -