답안 #579170

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
579170 2022-06-18T12:39:51 Z tengiz05 Cubeword (CEOI19_cubeword) C++17
0 / 100
1100 ms 4564 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;
    }
};

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[62][62] {}, cntp[62][62] {};
        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[62][62][62] {};
        
        for (int a = 0; a < 62; a++) {
            for (int b = 0; b < 62; b++) {
                for (int c = 0; c < 62; c++) {
                    for (int x = 0; x < 62; 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;
                    }
                }
            }
        }
        
        Z ans = 0;
        
        for (int a = 0; a < 62; a++) {
            for (int b = 0; b < 62; b++) {
                for (int c = 0; c < 62; c++) {
                    for (int d = 0; d < 62; d++) {
                        ans += dp[a][b][c] * dp[a][c][d] * dp[a][b][d] * dp[b][c][d];
                    }
                }
            }
        }
        
        return ans;
    };
    
    Z ans = 0;
    for (int k = 3; k <= 10; k++) {
        ans += work(s[k]);
    }
    
    cout << ans.val() << "\n";
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1190 ms 4564 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1190 ms 4564 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1190 ms 4564 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1190 ms 4564 KB Time limit exceeded
2 Halted 0 ms 0 KB -