Submission #579280

# Submission time Handle Problem Language Result Execution time Memory
579280 2022-06-18T17:48:07 Z tengiz05 Cubeword (CEOI19_cubeword) C++17
100 / 100
504 ms 6688 KB
#include <bits/stdc++.h>
 
using namespace std;
using i64 = long long;
 
const string ex = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 
constexpr int P = 998244353;
using i64 = long long;
// assume -P <= x < 2P
int norm(int x) {
    if (x < 0) {
        x += P;
    }
    if (x >= P) {
        x -= P;
    }
    return x;
}
template<class T>
T power(T a, int b) {
    T res = 1;
    for (; b; b /= 2, a *= a) {
        if (b % 2) {
            res *= a;
        }
    }
    return res;
}
struct Z {
    int x;
    Z(int x = 0) : x(norm(x)) {}
    int val() const {
        return x;
    }
    Z operator-() const {
        return Z(norm(P - x));
    }
    Z inv() const {
        assert(x != 0);
        return power(*this, P - 2);
    }
    Z &operator*=(const Z &rhs) {
        x = i64(x) * rhs.x % P;
        return *this;
    }
    Z &operator+=(const Z &rhs) {
        x = norm(x + rhs.x);
        return *this;
    }
    Z &operator-=(const Z &rhs) {
        x = norm(x - rhs.x);
        return *this;
    }
    Z &operator/=(const Z &rhs) {
        return *this *= rhs.inv();
    }
    friend Z operator*(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res *= rhs;
        return res;
    }
    friend Z operator+(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res += rhs;
        return res;
    }
    friend Z operator-(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res -= rhs;
        return res;
    }
    friend Z operator/(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res /= rhs;
        return res;
    }
};
 
constexpr int K = 62;
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n;
    cin >> n;
    
    vector<vector<string>> s(11);
    for (int i = 0; i < n; i++) {
        string g;
        cin >> g;
        string t(g.rbegin(), g.rend());
        if (t < g)
            g = t;
        s[g.size()].push_back(g);
    }
    
    auto work = [&](vector<string> &s) {
        sort(s.begin(), s.end());
        s.erase(unique(s.begin(), s.end()), s.end());
        
        int n = s.size();
        Z cnt[K][K] {}, cntp[K][K] {};
        for (int i = 0; i < n; i++) {
            int x = ex.find(s[i][0]);
            int y = ex.find(s[i].back());
            string t(s[i].rbegin(), s[i].rend());
            if (s[i] == t) {
                cntp[x][y] += 1;
            } else {
                cnt[x][y] += 1;
                cnt[y][x] += 1;
            }
        }
        
        Z dp[K][K][K] {};
        
        for (int a = 0; a < K; a++) {
            for (int b = a; b < K; b++) {
                for (int c = b; c < K; c++) {
                    for (int x = 0; x < K; x++) {
                        Z res = 1;
                        res *= ((a == x) * cntp[a][x]) + cnt[a][x];
                        res *= ((b == x) * cntp[b][x]) + cnt[b][x];
                        res *= ((c == x) * cntp[c][x]) + cnt[c][x];
                        dp[a][b][c] += res;
                    }
                    dp[a][c][b] = dp[b][a][c] = dp[b][c][a] = dp[c][a][b] = dp[c][b][a] = dp[a][b][c];
                }
            }
        }
        
        Z ans = 0;
        
        Z f[K][K][K];
        for (int a = 0; a < K; a++) {
            for (int b = a; b < K; b++) {
                for (int c = b; c < K; c++) {
                    for (int d = 0; d < K; d++) {
                        f[a][b][c] += dp[a][b][d] * dp[a][c][d] * dp[b][c][d];
                    }
                    f[a][c][b] = f[b][a][c] = f[b][c][a] = f[c][a][b] = f[c][b][a] = f[a][b][c];
                }
            }
        }
        
        for (int a = 0; a < K; a++) {
            for (int b = 0; b < K; b++) {
                for (int c = 0; c < K; c++) {
                    ans += dp[a][b][c] * f[a][b][c];
                }
            }
        }
        
        return ans;
    };
    
    Z ans = 0;
    for (int k = 3; k <= 10; k++) {
        ans += work(s[k]);
    }
    
    cout << ans.val() << "\n";
    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 454 ms 6260 KB Output is correct
2 Correct 470 ms 6364 KB Output is correct
3 Correct 448 ms 6576 KB Output is correct
4 Correct 491 ms 6548 KB Output is correct
5 Correct 477 ms 6480 KB Output is correct
6 Correct 451 ms 6460 KB Output is correct
7 Correct 487 ms 6688 KB Output is correct
8 Correct 460 ms 6396 KB Output is correct
9 Correct 476 ms 6408 KB Output is correct
10 Correct 480 ms 6436 KB Output is correct
11 Correct 468 ms 6388 KB Output is correct
12 Correct 454 ms 6540 KB Output is correct
13 Correct 499 ms 6304 KB Output is correct
14 Correct 492 ms 6572 KB Output is correct
15 Correct 473 ms 6684 KB Output is correct
16 Correct 435 ms 6400 KB Output is correct
17 Correct 504 ms 6608 KB Output is correct
18 Correct 467 ms 6360 KB Output is correct
19 Correct 434 ms 6368 KB Output is correct
20 Correct 466 ms 6440 KB Output is correct
21 Correct 467 ms 6400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 454 ms 6260 KB Output is correct
2 Correct 470 ms 6364 KB Output is correct
3 Correct 448 ms 6576 KB Output is correct
4 Correct 491 ms 6548 KB Output is correct
5 Correct 477 ms 6480 KB Output is correct
6 Correct 451 ms 6460 KB Output is correct
7 Correct 487 ms 6688 KB Output is correct
8 Correct 460 ms 6396 KB Output is correct
9 Correct 476 ms 6408 KB Output is correct
10 Correct 480 ms 6436 KB Output is correct
11 Correct 468 ms 6388 KB Output is correct
12 Correct 454 ms 6540 KB Output is correct
13 Correct 499 ms 6304 KB Output is correct
14 Correct 492 ms 6572 KB Output is correct
15 Correct 473 ms 6684 KB Output is correct
16 Correct 435 ms 6400 KB Output is correct
17 Correct 504 ms 6608 KB Output is correct
18 Correct 467 ms 6360 KB Output is correct
19 Correct 434 ms 6368 KB Output is correct
20 Correct 466 ms 6440 KB Output is correct
21 Correct 467 ms 6400 KB Output is correct
22 Correct 456 ms 6416 KB Output is correct
23 Correct 457 ms 6540 KB Output is correct
24 Correct 446 ms 6264 KB Output is correct
25 Correct 445 ms 6392 KB Output is correct
26 Correct 467 ms 6440 KB Output is correct
27 Correct 468 ms 6384 KB Output is correct
28 Correct 428 ms 6536 KB Output is correct
29 Correct 484 ms 6272 KB Output is correct
30 Correct 445 ms 6564 KB Output is correct
31 Correct 423 ms 6484 KB Output is correct
32 Correct 476 ms 6472 KB Output is correct
33 Correct 462 ms 6524 KB Output is correct
34 Correct 436 ms 6420 KB Output is correct
35 Correct 443 ms 6568 KB Output is correct
36 Correct 473 ms 6540 KB Output is correct
37 Correct 454 ms 6364 KB Output is correct
38 Correct 439 ms 6200 KB Output is correct
39 Correct 459 ms 6096 KB Output is correct
40 Correct 454 ms 6368 KB Output is correct
41 Correct 457 ms 6268 KB Output is correct
42 Correct 442 ms 6284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 454 ms 6260 KB Output is correct
2 Correct 470 ms 6364 KB Output is correct
3 Correct 448 ms 6576 KB Output is correct
4 Correct 491 ms 6548 KB Output is correct
5 Correct 477 ms 6480 KB Output is correct
6 Correct 451 ms 6460 KB Output is correct
7 Correct 487 ms 6688 KB Output is correct
8 Correct 460 ms 6396 KB Output is correct
9 Correct 476 ms 6408 KB Output is correct
10 Correct 480 ms 6436 KB Output is correct
11 Correct 468 ms 6388 KB Output is correct
12 Correct 454 ms 6540 KB Output is correct
13 Correct 499 ms 6304 KB Output is correct
14 Correct 492 ms 6572 KB Output is correct
15 Correct 473 ms 6684 KB Output is correct
16 Correct 435 ms 6400 KB Output is correct
17 Correct 504 ms 6608 KB Output is correct
18 Correct 467 ms 6360 KB Output is correct
19 Correct 434 ms 6368 KB Output is correct
20 Correct 466 ms 6440 KB Output is correct
21 Correct 467 ms 6400 KB Output is correct
22 Correct 456 ms 6416 KB Output is correct
23 Correct 457 ms 6540 KB Output is correct
24 Correct 446 ms 6264 KB Output is correct
25 Correct 445 ms 6392 KB Output is correct
26 Correct 467 ms 6440 KB Output is correct
27 Correct 468 ms 6384 KB Output is correct
28 Correct 428 ms 6536 KB Output is correct
29 Correct 484 ms 6272 KB Output is correct
30 Correct 445 ms 6564 KB Output is correct
31 Correct 423 ms 6484 KB Output is correct
32 Correct 476 ms 6472 KB Output is correct
33 Correct 462 ms 6524 KB Output is correct
34 Correct 436 ms 6420 KB Output is correct
35 Correct 443 ms 6568 KB Output is correct
36 Correct 473 ms 6540 KB Output is correct
37 Correct 454 ms 6364 KB Output is correct
38 Correct 439 ms 6200 KB Output is correct
39 Correct 459 ms 6096 KB Output is correct
40 Correct 454 ms 6368 KB Output is correct
41 Correct 457 ms 6268 KB Output is correct
42 Correct 442 ms 6284 KB Output is correct
43 Correct 435 ms 6164 KB Output is correct
44 Correct 437 ms 6252 KB Output is correct
45 Correct 447 ms 6504 KB Output is correct
46 Correct 435 ms 6484 KB Output is correct
47 Correct 441 ms 6244 KB Output is correct
48 Correct 450 ms 6504 KB Output is correct
49 Correct 454 ms 6528 KB Output is correct
50 Correct 448 ms 6504 KB Output is correct
51 Correct 416 ms 6496 KB Output is correct
52 Correct 438 ms 6224 KB Output is correct
53 Correct 424 ms 6188 KB Output is correct
54 Correct 447 ms 6320 KB Output is correct
55 Correct 452 ms 6488 KB Output is correct
56 Correct 428 ms 6300 KB Output is correct
57 Correct 447 ms 6248 KB Output is correct
58 Correct 471 ms 6492 KB Output is correct
59 Correct 446 ms 6236 KB Output is correct
60 Correct 435 ms 6248 KB Output is correct
61 Correct 447 ms 6192 KB Output is correct
62 Correct 457 ms 6488 KB Output is correct
63 Correct 437 ms 6260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 454 ms 6260 KB Output is correct
2 Correct 470 ms 6364 KB Output is correct
3 Correct 448 ms 6576 KB Output is correct
4 Correct 491 ms 6548 KB Output is correct
5 Correct 477 ms 6480 KB Output is correct
6 Correct 451 ms 6460 KB Output is correct
7 Correct 487 ms 6688 KB Output is correct
8 Correct 460 ms 6396 KB Output is correct
9 Correct 476 ms 6408 KB Output is correct
10 Correct 480 ms 6436 KB Output is correct
11 Correct 468 ms 6388 KB Output is correct
12 Correct 454 ms 6540 KB Output is correct
13 Correct 499 ms 6304 KB Output is correct
14 Correct 492 ms 6572 KB Output is correct
15 Correct 473 ms 6684 KB Output is correct
16 Correct 435 ms 6400 KB Output is correct
17 Correct 504 ms 6608 KB Output is correct
18 Correct 467 ms 6360 KB Output is correct
19 Correct 434 ms 6368 KB Output is correct
20 Correct 466 ms 6440 KB Output is correct
21 Correct 467 ms 6400 KB Output is correct
22 Correct 456 ms 6416 KB Output is correct
23 Correct 457 ms 6540 KB Output is correct
24 Correct 446 ms 6264 KB Output is correct
25 Correct 445 ms 6392 KB Output is correct
26 Correct 467 ms 6440 KB Output is correct
27 Correct 468 ms 6384 KB Output is correct
28 Correct 428 ms 6536 KB Output is correct
29 Correct 484 ms 6272 KB Output is correct
30 Correct 445 ms 6564 KB Output is correct
31 Correct 423 ms 6484 KB Output is correct
32 Correct 476 ms 6472 KB Output is correct
33 Correct 462 ms 6524 KB Output is correct
34 Correct 436 ms 6420 KB Output is correct
35 Correct 443 ms 6568 KB Output is correct
36 Correct 473 ms 6540 KB Output is correct
37 Correct 454 ms 6364 KB Output is correct
38 Correct 439 ms 6200 KB Output is correct
39 Correct 459 ms 6096 KB Output is correct
40 Correct 454 ms 6368 KB Output is correct
41 Correct 457 ms 6268 KB Output is correct
42 Correct 442 ms 6284 KB Output is correct
43 Correct 435 ms 6164 KB Output is correct
44 Correct 437 ms 6252 KB Output is correct
45 Correct 447 ms 6504 KB Output is correct
46 Correct 435 ms 6484 KB Output is correct
47 Correct 441 ms 6244 KB Output is correct
48 Correct 450 ms 6504 KB Output is correct
49 Correct 454 ms 6528 KB Output is correct
50 Correct 448 ms 6504 KB Output is correct
51 Correct 416 ms 6496 KB Output is correct
52 Correct 438 ms 6224 KB Output is correct
53 Correct 424 ms 6188 KB Output is correct
54 Correct 447 ms 6320 KB Output is correct
55 Correct 452 ms 6488 KB Output is correct
56 Correct 428 ms 6300 KB Output is correct
57 Correct 447 ms 6248 KB Output is correct
58 Correct 471 ms 6492 KB Output is correct
59 Correct 446 ms 6236 KB Output is correct
60 Correct 435 ms 6248 KB Output is correct
61 Correct 447 ms 6192 KB Output is correct
62 Correct 457 ms 6488 KB Output is correct
63 Correct 437 ms 6260 KB Output is correct
64 Correct 436 ms 6492 KB Output is correct
65 Correct 437 ms 6332 KB Output is correct
66 Correct 454 ms 6292 KB Output is correct
67 Correct 444 ms 6236 KB Output is correct
68 Correct 446 ms 6232 KB Output is correct
69 Correct 431 ms 6496 KB Output is correct
70 Correct 459 ms 6508 KB Output is correct
71 Correct 444 ms 6192 KB Output is correct
72 Correct 447 ms 6232 KB Output is correct
73 Correct 432 ms 6436 KB Output is correct
74 Correct 445 ms 6236 KB Output is correct
75 Correct 439 ms 6244 KB Output is correct
76 Correct 446 ms 6232 KB Output is correct
77 Correct 452 ms 6292 KB Output is correct
78 Correct 462 ms 6484 KB Output is correct
79 Correct 445 ms 6240 KB Output is correct
80 Correct 451 ms 6612 KB Output is correct
81 Correct 445 ms 6224 KB Output is correct
82 Correct 451 ms 6352 KB Output is correct
83 Correct 448 ms 6252 KB Output is correct
84 Correct 431 ms 6500 KB Output is correct