結果

問題 No.840 ほむほむほむら
ユーザー legosukelegosuke
提出日時 2021-01-07 05:27:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 362 ms / 4,000 ms
コード長 11,340 bytes
コンパイル時間 2,591 ms
コンパイル使用メモリ 206,760 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-25 08:56:21
合計ジャッジ時間 5,004 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 8 ms
5,376 KB
testcase_03 AC 50 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 17 ms
5,376 KB
testcase_08 AC 91 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 26 ms
5,376 KB
testcase_13 AC 242 ms
5,376 KB
testcase_14 AC 31 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 5 ms
5,376 KB
testcase_17 AC 56 ms
5,376 KB
testcase_18 AC 310 ms
5,376 KB
testcase_19 AC 362 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 6 ms
5,376 KB
testcase_23 AC 351 ms
5,376 KB
testcase_24 AC 6 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 8 ms
5,376 KB
testcase_27 AC 344 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define int int64_t
using namespace std;

/**
 * @brief modint 構造体 (base)
 */
class modint_base {};

template <class T>
using is_modint = std::is_base_of<modint_base, T>;

/**
 * @brief 累乗 : $a^n\bmod{m}$
 * @note O(\log{n})
 */
std::uint32_t mod_pow(std::int64_t a, std::uint64_t n, std::uint32_t m) {
    a = (a % m + m) % m;
    std::uint64_t res = 1;
    while (n) {
        if (n & 1) (res *= a) %= m;
        (a *= a) %= m;
        n >>= 1;
    }
    return (std::uint32_t)res;
}

/**
 * @brief 拡張ユークリッドの互助法
 * @note O(\min\log(a,b))
 */
std::int64_t ext_gcd(std::int64_t a, std::int64_t b, std::int_least64_t& x, std::int_least64_t& y) {
    if (b == 0) { x = 1; y = 0; return a; }
    auto g = ext_gcd(b, a % b, y, x);
    y -= a / b * x;
    return g;
}

/**
 * @brief mod 上の基本演算
 */
template <typename T, typename M>
inline M mod(T a, M m) {
    return (a % m + m) % m;
}

template <typename T, typename U, typename M>
inline M add(T a, U b, M m) {
    return mod(mod(a) + mod(b));
}

template <typename T, typename U, typename M>
inline M sub(T a, U b, M m) {
    return mod(mod(a) - mod(b));
}

template <typename T, typename U, typename M>
inline M mul(T a, U b, M m) {
    return mod((__uint128_t)a * b, m);
}

/**
 * @brief 逆元 : $a^{-1}\bmod{m}$ (拡張ユークリッドの互助法)
 * @note O(\log{m})
 * @warning a と m は互いに素
 */
std::uint32_t mod_inv(std::int64_t a, std::uint32_t m) {
    std::int_least64_t x, y;
    auto g = ext_gcd(a, m, x, y);
    assert(g == 1);
    return mod(x, m);
}

/**
 * @brief modint 構造体 (静的 MOD)
 */
template <std::uint32_t MOD>
class static_modint : public modint_base {
    using mint = static_modint;

public:
    static_modint() = default;
    template <typename Integer>
    static_modint(const Integer& v) : _v((v % MOD + MOD) % MOD) {}

    std::uint32_t mod() const {
        return MOD;
    }
    std::uint32_t val() const {
        return _v;
    }

    template <typename Integer>
    mint& operator += (const Integer& rhs) {
        _v += mint(rhs)._v;
        if (_v >= MOD) _v -= MOD;
        return *this;
    }
    template <typename Integer>
    mint& operator -= (const Integer& rhs)  {
        _v -= mint(rhs)._v;
        if (_v >= MOD) _v += MOD;
        return *this;
    }
    template <typename Integer>
    mint& operator *= (const Integer& rhs)  {
        std::uint64_t tmp = _v;
        tmp *= mint(rhs)._v;
        _v = (std::uint32_t)(tmp % MOD);
        return *this;
    }
    template <typename Integer>
    mint& operator /= (const Integer& rhs)  {
        return *this *= mint(rhs).inv();
    }
    template <typename Integer>
    mint& operator = (const Integer& v) {
        static_assert(std::is_integral<Integer>::value);
        _v = (v % MOD + MOD) % MOD;
        return *this;
    }
    mint pow(std::uint64_t n) const {
        return mint(mod_pow(_v, n, MOD));
    }
    mint inv() const {
        return mint(mod_inv(_v, MOD));
    }
    mint operator - () const {
        return mint(_v ? MOD - _v : 0);
    }
    friend std::ostream& operator << (std::ostream& os, const static_modint<MOD>& rhs) {
        return os << rhs._v;
    };

protected:
    std::uint32_t _v;
};
using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;

template <std::uint32_t MOD>
const static_modint<MOD> operator + (const static_modint<MOD>& lhs, const static_modint<MOD>& rhs) {
    return static_modint<MOD>(lhs) += rhs;
}
template <std::uint32_t MOD, typename Integer>
const static_modint<MOD> operator + (const static_modint<MOD>& lhs, const Integer& rhs) {
    return static_modint<MOD>(lhs) += rhs;
}
template <std::uint32_t MOD, typename Integer>
const static_modint<MOD> operator + (const Integer& lhs, const static_modint<MOD>& rhs) {
    return static_modint<MOD>(rhs) += lhs;
}

template <std::uint32_t MOD>
const static_modint<MOD> operator - (const static_modint<MOD>& lhs, const static_modint<MOD>& rhs) {
    return static_modint<MOD>(lhs) -= rhs;
}
template <std::uint32_t MOD, typename Integer>
const static_modint<MOD> operator - (const static_modint<MOD>& lhs, const Integer& rhs) {
    return static_modint<MOD>(lhs) -= rhs;
}
template <std::uint32_t MOD, typename Integer>
const static_modint<MOD> operator - (const Integer& lhs, const static_modint<MOD>& rhs) {
    return static_modint<MOD>(rhs) -= lhs;
}

template <std::uint32_t MOD>
const static_modint<MOD> operator * (const static_modint<MOD>& lhs, const static_modint<MOD>& rhs) {
    return static_modint<MOD>(lhs) *= rhs;
}
template <std::uint32_t MOD, typename Integer>
const static_modint<MOD> operator * (const static_modint<MOD>& lhs, const Integer& rhs) {
    return static_modint<MOD>(lhs) *= rhs;
}
template <std::uint32_t MOD, typename Integer>
const static_modint<MOD> operator * (const Integer& lhs, const static_modint<MOD>& rhs) {
    static_assert(std::is_same<Integer, static_modint<MOD>>::value == false);
    return static_modint<MOD>(rhs) *= lhs;
}

template <std::uint32_t MOD>
const static_modint<MOD> operator / (const static_modint<MOD>& lhs, const static_modint<MOD>& rhs) {
    return static_modint<MOD>(lhs) /= rhs;
}
template <std::uint32_t MOD, typename Integer>
const static_modint<MOD> operator / (const static_modint<MOD>& lhs, const Integer& rhs) {
    return static_modint<MOD>(lhs) /= rhs;
}
template <std::uint32_t MOD, typename Integer>
const static_modint<MOD> operator / (const Integer& lhs, const static_modint<MOD>& rhs) {
    return static_modint<MOD>(rhs) /= lhs;
}

/**
 * @brief 行列 (base)
 */
class matrix_base {};

template <class T>
using is_matrix = std::is_base_of<matrix_base, T>;

/**
 * @brief 行列 (vector)
 */
template <class T>
class matrix_vector : matrix_base {
public:
    using value_type = T;

    matrix_vector() = default;
    explicit matrix_vector(std::uint32_t n, std::uint32_t m, T x = T(0)) { init(n, m, x); }

    std::uint32_t height() const {
        return _n;
    }
    std::uint32_t width() const {
        return _m;
    }
    void init(std::uint32_t n, std::uint32_t m, T x = T(0)) {
        _n = n; _m = m;
        _v.clear(); _v.assign(_n, std::vector<T>(_m, x));
    }
    const std::vector<T>& operator [] (std::uint32_t i) const {
        return (_v.at(i));
    }
    std::vector<T>& operator [] (std::uint32_t i) {
        return (_v.at(i));
    }

    friend std::ostream& operator << (std::ostream& os, const matrix_vector<T>& A) {
        for (std::uint32_t i = 0; i < A.height(); ++i) for (std::uint32_t j = 0; j < A.width(); ++j) {
            os << A[i][j] << " \n"[j + 1 == A.width()];
        }
        return os;
    }

protected:
    std::uint32_t _n, _m;
    std::vector<std::vector<T>> _v;
};

template <class T>
matrix_vector<T> operator + (const matrix_vector<T>& A, const T& x) {
    matrix_vector<T> res(A.height(), A.width());
    for (std::uint32_t i = 0; i < A.height(); ++i) for (std::uint32_t j = 0; j < A.width(); ++j) {
        res[i][j] = A[i][j] + x;
    }
    return res;
}
template <class T>
matrix_vector<T> operator + (const T& x, const matrix_vector<T>& A) {
    matrix_vector<T> res(A.height(), A.width());
    for (std::uint32_t i = 0; i < A.height(); ++i) for (std::uint32_t j = 0; j < A.width(); ++j) {
        res[i][j] = x + A[i][j];
    }
    return res;
}
template <class T>
matrix_vector<T> operator + (const matrix_vector<T>& A, const matrix_vector<T>& B) {
    assert(A.height() == B.height() && A.width() == B.width());
    matrix_vector<T> res(A.height(), A.width());
    for (std::uint32_t i = 0; i < A.height(); ++i) for (std::uint32_t j = 0; j < A.width(); ++j) {
        res[i][j] = A[i][j] + B[i][j];
    }
    return res;
}

template <class T>
matrix_vector<T> operator - (const matrix_vector<T>& A, const T& x) {
    matrix_vector<T> res(A.height(), A.width());
    for (std::uint32_t i = 0; i < A.height(); ++i) for (std::uint32_t j = 0; j < A.width(); ++j) {
        res[i][j] = A[i][j] - x;
    }
    return res;
}
template <class T>
matrix_vector<T> operator - (const T& x, const matrix_vector<T>& A) {
    matrix_vector<T> res(A.height(), A.width());
    for (std::uint32_t i = 0; i < A.height(); ++i) for (std::uint32_t j = 0; j < A.width(); ++j) {
        res[i][j] = x - A[i][j];
    }
    return res;
}
template <class T>
matrix_vector<T> operator - (const matrix_vector<T>& A, const matrix_vector<T>& B) {
    assert(A.height() == B.height() && A.width() == B.width());
    matrix_vector<T> res(A.height(), A.width());
    for (std::uint32_t i = 0; i < A.height(); ++i) for (std::uint32_t j = 0; j < A.width(); ++j) {
        res[i][j] = A[i][j] - B[i][j];
    }
    return res;
}

template <class T>
matrix_vector<T> operator * (const matrix_vector<T>& A, const T& x) {
    matrix_vector<T> res(A.height(), A.width());
    for (std::uint32_t i = 0; i < A.height(); ++i) for (std::uint32_t j = 0; j < A.width(); ++j) {
        res[i][j] = A[i][j] * x;
    }
    return res;
}
template <class T>
matrix_vector<T> operator * (const T& x, const matrix_vector<T>& A) {
    matrix_vector<T> res(A.height(), A.width());
    for (std::uint32_t i = 0; i < A.height(); ++i) for (std::uint32_t j = 0; j < A.width(); ++j) {
        res[i][j] = x * A[i][j];
    }
    return res;
}
template <class T>
std::vector<T> operator * (const matrix_vector<T>& A, const std::vector<T>& v) {
    assert(A.width() == (std::uint32_t)v.size());
    std::vector<T> u(A.height(), T(0));
    for (std::uint32_t i = 0; i < A.height(); ++i) for (std::uint32_t j = 0; j < A.width(); ++j) {
        u[i] = u[i] + A[i][j] * v[j];
    }
    return u;
}
template <class T>
matrix_vector<T> operator * (const matrix_vector<T>& A, const matrix_vector<T>& B) {
    assert(A.width() == B.height());
    matrix_vector<T> res(A.height(), B.width(), T(0));
    for (std::uint32_t i = 0; i < A.height(); ++i) for (std::uint32_t j = 0; j < B.width(); ++j) for (std::uint32_t k = 0; k < A.width(); ++k) {
        res[i][j] = res[i][j] + A[i][k] * B[k][j];
    }
    return res;
}

template <class T>
matrix_vector<T> operator / (const matrix_vector<T>& A, const T& x) {
    matrix_vector<T> res(A.height(), A.width());
    for (std::uint32_t i = 0; i < A.height(); ++i) for (std::uint32_t j = 0; j < A.width(); ++j) {
        res[i][j] = A[i][j] / x;
    }
    return res;
}
template <class T>
matrix_vector<T> operator ^ (matrix_vector<T> A, std::uint64_t n) {
    assert(A.height() == A.width());
    matrix_vector<T> B(A.height(), A.width());
    for (int i = 0; i < A.height(); ++i) B[i][i] = T(1);
    while (n) {
        if (n & 1) B = B * A;
        A = A * A;
        n >>= 1;
    }
    return B;
}

signed main() {
    int N, K; cin >> N >> K;
    matrix_vector<modint998244353> M(K * K * K, K * K * K);
    int K2 = K * K;
    for (int i = 0; i < K; ++i) for (int j = 0; j < K; ++j) for (int k = 0; k < K; ++k) {
        int src = K2 * i + K * j + k;
        M[K2 * ((i + 1) % K) + K * j + k][src] += 1;
        M[K2 * i + K * ((j + i) % K) + k][src] += 1;
        M[K2 * i + K * j + ((k + j) % K)][src] += 1;
    }
    M = M ^ N;

    vector<modint998244353> v(K * K * K);
    v[0] = 1;
    v = M * v;
    modint998244353 ans = 0;
    for (int i = 0; i < K * K; ++i) {
        ans += v[i * K].val();
    }
    cout << ans << endl;
}
0