답안 #572388

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
572388 2022-06-04T10:28:38 Z piOOE Cubeword (CEOI19_cubeword) C++17
84 / 100
554 ms 14112 KB
#include <bits/stdc++.h>

using namespace std;

#define sz(x) ((int)size(x))
#define all(x) begin(x), end(x)
#define trace(x) cout << #x << ": " << (x) << endl;

namespace Ment {
    template<typename T>
    T inverse(T a, T m) {
        T u = 0, v = 1;
        while (a != 0) {
            T t = m / a;
            m -= t * a;
            swap(a, m);
            u -= t * v;
            swap(u, v);
        }
        assert(m == 1);
        return u;
    }

    template<typename T>
    class Modular {
    public:
        using Type = typename decay<decltype(T::value)>::type;

        constexpr Modular() : value() {}

        template<typename U>
        Modular(const U &x) {
            value = normalize(x);
        }

        template<typename U>
        static Type normalize(const U &x) {
            Type v;
            if (-mod() <= x && x < mod()) v = static_cast<Type>(x);
            else v = static_cast<Type>(x % mod());
            if (v < 0) v += mod();
            return v;
        }

        const Type &operator()() const { return value; }

        template<typename U>
        explicit operator U() const { return static_cast<U>(value); }

        constexpr static Type mod() { return T::value; }

        Modular &operator+=(const Modular &other) {
            value += other.value - mod();
            value += (value >> 31) & mod();
            return *this;
        }

        Modular &operator-=(const Modular &other) {
            value -= other.value;
            value += (value >> 31) & mod();
            return *this;
        }

        template<typename U>
        Modular &operator+=(const U &other) { return *this += Modular(other); }

        template<typename U>
        Modular &operator-=(const U &other) { return *this -= Modular(other); }

        Modular &operator++() { return *this += 1; }

        Modular &operator--() { return *this -= 1; }

        Modular operator++(int) {
            Modular result(*this);
            *this += 1;
            return result;
        }

        Modular operator--(int) {
            Modular result(*this);
            *this -= 1;
            return result;
        }

        Modular operator-() const { return Modular(-value); }

        template<typename U = T>
        typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type &
        operator*=(const Modular &rhs) {
#ifdef _WIN32
            uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value);
            uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m;
            asm(
                    "divl %4; \n\t"
                    : "=a" (d), "=d" (m)
                    : "d" (xh), "a" (xl), "r" (mod())
                    );
            value = m;
#else
            value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value));
#endif
            return *this;
        }

        template<typename U = T>
        typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type &
        operator*=(const Modular &rhs) {
            int64_t q = static_cast<int64_t>(static_cast<long double>(value) * rhs.value / mod());
            value = normalize(value * rhs.value - q * mod());
            return *this;
        }

        template<typename U = T>
        typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type &
        operator*=(const Modular &rhs) {
            value = normalize(value * rhs.value);
            return *this;
        }

        Modular &operator/=(const Modular &other) { return *this *= Modular(inverse(other.value, mod())); }

        template<typename U>
        friend bool operator==(const Modular<U> &lhs, const Modular<U> &rhs);

        template<typename U>
        friend bool operator<(const Modular<U> &lhs, const Modular<U> &rhs);

        template<typename U>
        friend std::istream &operator>>(std::istream &stream, Modular<U> &number);

    private:
        Type value;
    };

    template<typename T>
    bool operator==(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value == rhs.value; }

    template<typename T, typename U>
    bool operator==(const Modular<T> &lhs, U rhs) { return lhs == Modular<T>(rhs); }

    template<typename T, typename U>
    bool operator==(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) == rhs; }

    template<typename T>
    bool operator!=(const Modular<T> &lhs, const Modular<T> &rhs) { return !(lhs == rhs); }

    template<typename T, typename U>
    bool operator!=(const Modular<T> &lhs, U rhs) { return !(lhs == rhs); }

    template<typename T, typename U>
    bool operator!=(U lhs, const Modular<T> &rhs) { return !(lhs == rhs); }

    template<typename T>
    bool operator<(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value < rhs.value; }

    template<typename T>
    Modular<T> operator+(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; }

    template<typename T, typename U>
    Modular<T> operator+(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) += rhs; }

    template<typename T, typename U>
    Modular<T> operator+(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; }

    template<typename T>
    Modular<T> operator-(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; }

    template<typename T, typename U>
    Modular<T> operator-(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) -= rhs; }

    template<typename T, typename U>
    Modular<T> operator-(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; }

    template<typename T>
    Modular<T> operator*(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; }

    template<typename T, typename U>
    Modular<T> operator*(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) *= rhs; }

    template<typename T, typename U>
    Modular<T> operator*(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; }

    template<typename T>
    Modular<T> operator/(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; }

    template<typename T, typename U>
    Modular<T> operator/(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) /= rhs; }

    template<typename T, typename U>
    Modular<T> operator/(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; }

    template<typename T, typename U>
    Modular<T> power(const Modular<T> &a, const U &b) {
        assert(b >= 0);
        Modular<T> x = a, res = 1;
        U p = b;
        while (p > 0) {
            if (p & 1) res *= x;
            x *= x;
            p >>= 1;
        }
        return res;
    }

    template<typename T>
    string to_string(const Modular<T> &number) {
        return to_string(number());
    }

    template<typename T>
    std::ostream &operator<<(std::ostream &stream, const Modular<T> &number) {
        return stream << number();
    }

    template<typename T>
    std::istream &operator>>(std::istream &stream, Modular<T> &number) {
        typename common_type<typename Modular<T>::Type, int64_t>::type x;
        stream >> x;
        number.value = Modular<T>::normalize(x);
        return stream;
    }

    constexpr int md = 998244353;
    using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;
}
using Ment::Mint;


typedef long long ll;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

int rand(int l, int r) { return (int) ((ll) rnd() % (r - l + 1)) + l; }

const int N = 8, A = 32, L = 11;
const ll infL = 3e18;

int g[N][3] = {{1, 2, 4},
               {0, 3, 5},
               {0, 3, 6},
               {1, 2, 7},
               {0, 5, 6},
               {1, 4, 7},
               {2, 4, 7},
               {3, 5, 6}};

int n;
Mint cnt[A][A][L], dp2[A][A][A], dp7[A][A][A], dp1[A][A][A], dp4[A][A][A];
Mint ans = 0;
int len = 0, used[N];

int get(char c) {
    if ('a' <= c && c <= 'z') {
        return c - 'a';
    }
    if ('A' <= c && c <= 'Z') {
        return (c - 'A') + 16;
    }
    return (c - '0') + 52;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    vector<string> stt;
    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        string ss = s;
        reverse(all(ss));
        stt.push_back(ss);
        stt.push_back(s);
    }
    sort(all(stt));
    stt.resize(unique(all(stt)) - begin(stt));
    for (string s: stt) {
        ++cnt[get(s[0])][get(s.back())][sz(s)];
    }
    for (len = 1; len < L; ++len) {
        {
            //2
            memset(used, -1, sizeof(used));
            for (int x0 = 0; x0 < A; ++x0) {
                used[0] = x0;
                for (int x3 = 0; x3 < A; ++x3) {
                    used[3] = x3;
                    for (int x6 = 0; x6 < A; ++x6) {
                        used[6] = x6;
                        Mint sum = 0;
                        for (int x = 0; x < A; ++x) {
                            Mint now = 1;
                            for (int to: g[2]) {
                                now *= cnt[x][used[to]][len];
                            }
                            sum = (sum + now);
                        }
                        dp2[x0][x3][x6] = sum;
                    }
                }
            }
        }
        {
            //1
            memset(used, -1, sizeof(used));
            for (int x0 = 0; x0 < A; ++x0) {
                used[0] = x0;
                for (int x3 = 0; x3 < A; ++x3) {
                    used[3] = x3;
                    for (int x5 = 0; x5 < A; ++x5) {
                        used[5] = x5;
                        Mint sum = 0;
                        for (int x = 0; x < A; ++x) {
                            Mint now = 1;
                            for (int to: g[1]) {
                                now *= cnt[x][used[to]][len];
                            }
                            sum += now;
                        }
                        dp1[x0][x3][x5] = sum;
                    }
                }
            }
        }
        {
            //7
            memset(used, -1, sizeof(used));
            for (int x6 = 0; x6 < A; ++x6) {
                used[6] = x6;
                for (int x3 = 0; x3 < A; ++x3) {
                    used[3] = x3;
                    for (int x5 = 0; x5 < A; ++x5) {
                        used[5] = x5;
                        Mint sum = 0;
                        for (int x = 0; x < A; ++x) {
                            Mint now = 1;
                            for (int to: g[7]) {
                                now *= cnt[x][used[to]][len];
                            }
                            sum += now;
                        }
                        dp7[x3][x5][x6] = sum;
                    }
                }
            }
        }
        {
            //4
            memset(used, -1, sizeof(used));
            for (int x0 = 0; x0 < A; ++x0) {
                used[0] = x0;
                for (int x6 = 0; x6 < A; ++x6) {
                    used[6] = x6;
                    for (int x5 = 0; x5 < A; ++x5) {
                        used[5] = x5;
                        Mint sum = 0;
                        for (int x = 0; x < A; ++x) {
                            Mint now = 1;
                            for (int to: g[4]) {
                                now *= cnt[x][used[to]][len];
                            }
                            sum += now;
                        }
                        dp4[x0][x5][x6] = sum;
                    }
                }
            }
        }
        for (int x0 = 0; x0 < A; ++x0) {
            for (int x3 = 0; x3 < A; ++x3) {
                for (int x5 = 0; x5 < A; ++x5) {
                    for (int x6 = 0; x6 < A; ++x6) {
                        ans = (ans + dp4[x0][x5][x6] * dp7[x3][x5][x6] * dp1[x0][x3][x5] *
                                     dp2[x0][x3][x6]);
                    }
                }
            }
        }
    }
    cout << ans;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 464 ms 8648 KB Output is correct
2 Correct 484 ms 8648 KB Output is correct
3 Correct 465 ms 8648 KB Output is correct
4 Correct 464 ms 8648 KB Output is correct
5 Correct 476 ms 8648 KB Output is correct
6 Correct 446 ms 8580 KB Output is correct
7 Correct 486 ms 8648 KB Output is correct
8 Correct 446 ms 8648 KB Output is correct
9 Correct 472 ms 8648 KB Output is correct
10 Correct 473 ms 8668 KB Output is correct
11 Correct 453 ms 8648 KB Output is correct
12 Correct 468 ms 8648 KB Output is correct
13 Correct 469 ms 8648 KB Output is correct
14 Correct 461 ms 8776 KB Output is correct
15 Correct 488 ms 8648 KB Output is correct
16 Correct 475 ms 8664 KB Output is correct
17 Correct 456 ms 8668 KB Output is correct
18 Correct 455 ms 8648 KB Output is correct
19 Correct 464 ms 8648 KB Output is correct
20 Correct 468 ms 8648 KB Output is correct
21 Correct 464 ms 8648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 464 ms 8648 KB Output is correct
2 Correct 484 ms 8648 KB Output is correct
3 Correct 465 ms 8648 KB Output is correct
4 Correct 464 ms 8648 KB Output is correct
5 Correct 476 ms 8648 KB Output is correct
6 Correct 446 ms 8580 KB Output is correct
7 Correct 486 ms 8648 KB Output is correct
8 Correct 446 ms 8648 KB Output is correct
9 Correct 472 ms 8648 KB Output is correct
10 Correct 473 ms 8668 KB Output is correct
11 Correct 453 ms 8648 KB Output is correct
12 Correct 468 ms 8648 KB Output is correct
13 Correct 469 ms 8648 KB Output is correct
14 Correct 461 ms 8776 KB Output is correct
15 Correct 488 ms 8648 KB Output is correct
16 Correct 475 ms 8664 KB Output is correct
17 Correct 456 ms 8668 KB Output is correct
18 Correct 455 ms 8648 KB Output is correct
19 Correct 464 ms 8648 KB Output is correct
20 Correct 468 ms 8648 KB Output is correct
21 Correct 464 ms 8648 KB Output is correct
22 Correct 480 ms 8648 KB Output is correct
23 Correct 554 ms 8648 KB Output is correct
24 Correct 547 ms 8648 KB Output is correct
25 Correct 513 ms 8648 KB Output is correct
26 Correct 471 ms 8648 KB Output is correct
27 Correct 497 ms 8684 KB Output is correct
28 Correct 504 ms 8628 KB Output is correct
29 Correct 484 ms 8672 KB Output is correct
30 Correct 513 ms 8648 KB Output is correct
31 Correct 520 ms 8680 KB Output is correct
32 Correct 481 ms 8616 KB Output is correct
33 Correct 480 ms 8684 KB Output is correct
34 Correct 507 ms 8616 KB Output is correct
35 Correct 467 ms 8648 KB Output is correct
36 Correct 445 ms 8648 KB Output is correct
37 Correct 449 ms 8648 KB Output is correct
38 Correct 460 ms 8648 KB Output is correct
39 Correct 460 ms 8648 KB Output is correct
40 Correct 471 ms 8648 KB Output is correct
41 Correct 462 ms 8660 KB Output is correct
42 Correct 442 ms 8576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 464 ms 8648 KB Output is correct
2 Correct 484 ms 8648 KB Output is correct
3 Correct 465 ms 8648 KB Output is correct
4 Correct 464 ms 8648 KB Output is correct
5 Correct 476 ms 8648 KB Output is correct
6 Correct 446 ms 8580 KB Output is correct
7 Correct 486 ms 8648 KB Output is correct
8 Correct 446 ms 8648 KB Output is correct
9 Correct 472 ms 8648 KB Output is correct
10 Correct 473 ms 8668 KB Output is correct
11 Correct 453 ms 8648 KB Output is correct
12 Correct 468 ms 8648 KB Output is correct
13 Correct 469 ms 8648 KB Output is correct
14 Correct 461 ms 8776 KB Output is correct
15 Correct 488 ms 8648 KB Output is correct
16 Correct 475 ms 8664 KB Output is correct
17 Correct 456 ms 8668 KB Output is correct
18 Correct 455 ms 8648 KB Output is correct
19 Correct 464 ms 8648 KB Output is correct
20 Correct 468 ms 8648 KB Output is correct
21 Correct 464 ms 8648 KB Output is correct
22 Correct 480 ms 8648 KB Output is correct
23 Correct 554 ms 8648 KB Output is correct
24 Correct 547 ms 8648 KB Output is correct
25 Correct 513 ms 8648 KB Output is correct
26 Correct 471 ms 8648 KB Output is correct
27 Correct 497 ms 8684 KB Output is correct
28 Correct 504 ms 8628 KB Output is correct
29 Correct 484 ms 8672 KB Output is correct
30 Correct 513 ms 8648 KB Output is correct
31 Correct 520 ms 8680 KB Output is correct
32 Correct 481 ms 8616 KB Output is correct
33 Correct 480 ms 8684 KB Output is correct
34 Correct 507 ms 8616 KB Output is correct
35 Correct 467 ms 8648 KB Output is correct
36 Correct 445 ms 8648 KB Output is correct
37 Correct 449 ms 8648 KB Output is correct
38 Correct 460 ms 8648 KB Output is correct
39 Correct 460 ms 8648 KB Output is correct
40 Correct 471 ms 8648 KB Output is correct
41 Correct 462 ms 8660 KB Output is correct
42 Correct 442 ms 8576 KB Output is correct
43 Correct 490 ms 8648 KB Output is correct
44 Correct 491 ms 9288 KB Output is correct
45 Correct 505 ms 9288 KB Output is correct
46 Correct 488 ms 9284 KB Output is correct
47 Correct 474 ms 9276 KB Output is correct
48 Correct 509 ms 9236 KB Output is correct
49 Correct 490 ms 9276 KB Output is correct
50 Correct 512 ms 9288 KB Output is correct
51 Correct 480 ms 9288 KB Output is correct
52 Correct 494 ms 9280 KB Output is correct
53 Correct 486 ms 9288 KB Output is correct
54 Correct 477 ms 9288 KB Output is correct
55 Correct 472 ms 9288 KB Output is correct
56 Correct 484 ms 9416 KB Output is correct
57 Correct 472 ms 9288 KB Output is correct
58 Correct 484 ms 9288 KB Output is correct
59 Correct 481 ms 9288 KB Output is correct
60 Correct 471 ms 9220 KB Output is correct
61 Correct 477 ms 9288 KB Output is correct
62 Correct 480 ms 9228 KB Output is correct
63 Correct 487 ms 9288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 464 ms 8648 KB Output is correct
2 Correct 484 ms 8648 KB Output is correct
3 Correct 465 ms 8648 KB Output is correct
4 Correct 464 ms 8648 KB Output is correct
5 Correct 476 ms 8648 KB Output is correct
6 Correct 446 ms 8580 KB Output is correct
7 Correct 486 ms 8648 KB Output is correct
8 Correct 446 ms 8648 KB Output is correct
9 Correct 472 ms 8648 KB Output is correct
10 Correct 473 ms 8668 KB Output is correct
11 Correct 453 ms 8648 KB Output is correct
12 Correct 468 ms 8648 KB Output is correct
13 Correct 469 ms 8648 KB Output is correct
14 Correct 461 ms 8776 KB Output is correct
15 Correct 488 ms 8648 KB Output is correct
16 Correct 475 ms 8664 KB Output is correct
17 Correct 456 ms 8668 KB Output is correct
18 Correct 455 ms 8648 KB Output is correct
19 Correct 464 ms 8648 KB Output is correct
20 Correct 468 ms 8648 KB Output is correct
21 Correct 464 ms 8648 KB Output is correct
22 Correct 480 ms 8648 KB Output is correct
23 Correct 554 ms 8648 KB Output is correct
24 Correct 547 ms 8648 KB Output is correct
25 Correct 513 ms 8648 KB Output is correct
26 Correct 471 ms 8648 KB Output is correct
27 Correct 497 ms 8684 KB Output is correct
28 Correct 504 ms 8628 KB Output is correct
29 Correct 484 ms 8672 KB Output is correct
30 Correct 513 ms 8648 KB Output is correct
31 Correct 520 ms 8680 KB Output is correct
32 Correct 481 ms 8616 KB Output is correct
33 Correct 480 ms 8684 KB Output is correct
34 Correct 507 ms 8616 KB Output is correct
35 Correct 467 ms 8648 KB Output is correct
36 Correct 445 ms 8648 KB Output is correct
37 Correct 449 ms 8648 KB Output is correct
38 Correct 460 ms 8648 KB Output is correct
39 Correct 460 ms 8648 KB Output is correct
40 Correct 471 ms 8648 KB Output is correct
41 Correct 462 ms 8660 KB Output is correct
42 Correct 442 ms 8576 KB Output is correct
43 Correct 490 ms 8648 KB Output is correct
44 Correct 491 ms 9288 KB Output is correct
45 Correct 505 ms 9288 KB Output is correct
46 Correct 488 ms 9284 KB Output is correct
47 Correct 474 ms 9276 KB Output is correct
48 Correct 509 ms 9236 KB Output is correct
49 Correct 490 ms 9276 KB Output is correct
50 Correct 512 ms 9288 KB Output is correct
51 Correct 480 ms 9288 KB Output is correct
52 Correct 494 ms 9280 KB Output is correct
53 Correct 486 ms 9288 KB Output is correct
54 Correct 477 ms 9288 KB Output is correct
55 Correct 472 ms 9288 KB Output is correct
56 Correct 484 ms 9416 KB Output is correct
57 Correct 472 ms 9288 KB Output is correct
58 Correct 484 ms 9288 KB Output is correct
59 Correct 481 ms 9288 KB Output is correct
60 Correct 471 ms 9220 KB Output is correct
61 Correct 477 ms 9288 KB Output is correct
62 Correct 480 ms 9228 KB Output is correct
63 Correct 487 ms 9288 KB Output is correct
64 Runtime error 73 ms 14112 KB Execution killed with signal 11
65 Halted 0 ms 0 KB -