結果

問題 No.2156 ぞい文字列
ユーザー ruthenruthen
提出日時 2022-12-10 16:55:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 4,391 bytes
コンパイル時間 2,156 ms
コンパイル使用メモリ 199,828 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 00:38:33
合計ジャッジ時間 2,808 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#ifdef _RUTHEN
#include <debug.hpp>
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

#include <atcoder/modint>
using mint = atcoder::modint998244353;
ostream &operator<<(ostream &os, const mint &p) { return os << p.val(); }


template <class T, size_t n, size_t m = n> struct static_matrix {
    std::array<std::array<T, m>, n> A;

    static_matrix() : A{{}} {}

    static_matrix(T val) : A{{}} {
        for (int i = 0; i < (int)n; i++) A[i].fill(val);
    }

    size_t size() const { return n; }

    int row() const { return (int)n; }

    int col() const { return (int)m; }

    inline const std::array<T, m> &operator[](int i) const { return A[i]; }  // read

    inline std::array<T, m> &operator[](int i) { return A[i]; }  // write

    static static_matrix E() {
        assert(n == m);
        static_matrix ret;
        for (int i = 0; i < (int)n; i++) ret[i][i] = T(1);
        return ret;
    }

    static_matrix &operator+=(const static_matrix &B) {
        int N = row(), M = col();
        assert(N == B.row() && M == B.col());
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                (*this)[i][j] += B[i][j];
            }
        }
        return (*this);
    }

    static_matrix &operator-=(const static_matrix &B) {
        int N = row(), M = col();
        assert(N == B.row() && M == B.col());
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                (*this)[i][j] -= B[i][j];
            }
        }
        return (*this);
    }

    static_matrix &operator*=(const static_matrix &B) {
        int N = row(), M = B.col(), L = B.row();
        assert(L == col());
        static_matrix C;
        for (int i = 0; i < N; i++) {
            for (int k = 0; k < L; k++) {
                for (int j = 0; j < M; j++) {
                    C[i][j] += (*this)[i][k] * B[k][j];
                }
            }
        }
        A.swap(C.A);
        return (*this);
    }

    static_matrix pow(long long k) {
        assert(row() == col());
        static_matrix B = static_matrix::E(), X = (*this);
        while (k) {
            if (k & 1) B *= X;
            X *= X;
            k >>= 1;
        }
        A.swap(B.A);
        return (*this);
    }

    static_matrix operator+(const static_matrix &B) { return ((*this) += B); }

    static_matrix operator-(const static_matrix &B) { return ((*this) -= B); }

    static_matrix operator*(const static_matrix &B) { return ((*this) *= B); }

    friend std::ostream &operator<<(std::ostream &os, static_matrix &A) {
        int N = A.row(), M = A.col();
        for (int i = 0; i < N; i++) {
            os << '[';
            for (int j = 0; j < M; j++) os << A[i][j] << " \n"[j == M - 1];
        }
        return (os);
    }

    static_matrix &operator+=(const T &k) {
        int N = row(), M = col();
        for (int i = 0; i < N; i++)
            for (int j = 0; j < M; j++) (*this)[i][j] += k;
        return (*this);
    }

    static_matrix &operator-=(const T &k) {
        int N = row(), M = col();
        for (int i = 0; i < N; i++)
            for (int j = 0; j < M; j++) (*this)[i][j] -= k;
        return (*this);
    }

    static_matrix &operator*=(const T &k) {
        int N = row(), M = col();
        for (int i = 0; i < N; i++)
            for (int j = 0; j < M; j++) (*this)[i][j] *= k;
        return (*this);
    }

    static_matrix &operator/=(const T &k) {
        int N = row(), M = col();
        for (int i = 0; i < N; i++)
            for (int j = 0; j < M; j++) (*this)[i][j] /= k;
        return (*this);
    }

    static_matrix operator+(const T &k) { return ((*this) += k); }

    static_matrix operator-(const T &k) { return ((*this) -= k); }

    static_matrix operator*(const T &k) { return ((*this) *= k); }

    static_matrix operator/(const T &k) { return ((*this) /= k); }
};

/**
 * @brief Static Matrix (行列, サイズ固定)
 * @docs docs/data_structure/static_matrix.md
 */

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll N;
    cin >> N;
    static_matrix<mint, 2, 2> dp;
    dp[0][0] = dp[0][1] = dp[1][0] = 1;
    dp = dp.pow(N - 1);
    mint ans = dp[0][0] + dp[1][0] - 1;
    cout << ans << '\n';
    return 0;
}
0