結果

問題 No.2156 ぞい文字列
ユーザー みここみここ
提出日時 2022-12-10 18:25:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 4,759 bytes
コンパイル時間 1,177 ms
コンパイル使用メモリ 81,668 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-23 00:41:12
合計ジャッジ時間 1,606 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cassert>
#include <vector>
using namespace std;
typedef long long ll;

const ll MOD = 998244353;

struct mint
{
    int val;

    mint() : val(0) {}

    mint(long long v) : val(v)
    {
        if (abs(val) >= MOD)
        {
            val %= MOD;
        }
        if (val < 0)
        {
            val += MOD;
        }
    }

    mint &operator++()
    {
        val++;
        if (val == MOD)
        {
            val = 0;
        }
        return *this;
    }

    mint &operator--()
    {
        if (val == 0)
        {
            val = MOD;
        }
        val--;
        return *this;
    }

    mint &operator+=(const mint &x)
    {
        val += x.val;
        if (val >= MOD)
        {
            val -= MOD;
        }
        return *this;
    }

    mint &operator-=(const mint &x)
    {
        val -= x.val;
        if (val < 0)
        {
            val += MOD;
        }
        return *this;
    }

    mint &operator*=(const mint &x)
    {
        val = (int)((long long)val * x.val % MOD);
        return *this;
    }

    mint &operator/=(const mint &x)
    {
        *this *= x.inv();
        return *this;
    }

    mint operator-()
    {
        return mint() - *this;
    }

    mint pow(long long n) const
    {
        mint x = 1, r = *this;
        while (n)
        {
            if (n & 1)
            {
                x *= r;
            }
            r *= r;
            n >>= 1;
        }
        return x;
    }

    mint inv() const
    {
        return pow(MOD - 2);
    }

    friend mint operator+(const mint &x, const mint &y)
    {
        return mint(x) += y;
    }

    friend mint operator-(const mint &x, const mint &y)
    {
        return mint(x) -= y;
    }

    friend mint operator*(const mint &x, const mint &y)
    {
        return mint(x) *= y;
    }

    friend mint operator/(const mint &x, const mint &y)
    {
        return mint(x) /= y;
    }

    friend bool operator==(const mint &x, const mint &y)
    {
        return x.val == y.val;
    }

    friend bool operator!=(const mint &x, const mint &y)
    {
        return x.val != y.val;
    }

    friend std::ostream &operator<<(std::ostream &os, const mint &x)
    {
        return os << x.val;
    }

    friend std::istream &operator>>(std::istream &is, mint &x)
    {
        int v;
        is >> v;
        x = mint(v);
        return is;
    }
};

template <typename T>
struct Matrix
{
    int n, m;
    std::vector<std::vector<T>> a;

    Matrix() {}

    Matrix(int n, int m) : n(n), m(m), a(n, std::vector<T>(m)) {}

    Matrix(int n) : n(n), m(n), a(n, std::vector<T>(n)) {}

    static Matrix<T> I(int n)
    {
        Matrix<T> res(n);
        for (int i = 0; i < n; i++)
        {
            res[i][i] = 1;
        }
        return res;
    }

    Matrix<T> &operator+=(const Matrix<T> &b)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                (*this)[i][j] += b[i][j];
            }
        }
        return *this;
    }

    Matrix<T> &operator-=(const Matrix<T> &b)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                (*this)[i][j] -= b[i][j];
            }
        }
        return *this;
    }

    Matrix<T> &operator*=(const Matrix<T> &b)
    {
        assert(m == b.n);
        std::vector<std::vector<T>> c(n, std::vector<T>(b.m));
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < b.m; j++)
            {
                for (int k = 0; k < m; k++)
                {
                    c[i][j] += (*this)[i][k] * b[k][j];
                }
            }
        }
        m = b.m;
        a.swap(c);
        return *this;
    }

    Matrix<T> &operator^=(long long k)
    {
        Matrix<T> b = Matrix<T>::I(n);
        while (k)
        {
            if (k & 1)
            {
                b *= *this;
            }
            *this *= *this;
            k >>= 1;
        }
        a.swap(b.a);
        return *this;
    }

    Matrix<T> operator+(const Matrix<T> &a)
    {
        return (Matrix<T>(*this) += a);
    }

    Matrix<T> operator-(const Matrix<T> &a)
    {
        return (Matrix<T>(*this) -= a);
    }

    Matrix<T> operator*(const Matrix<T> &a)
    {
        return (Matrix<T>(*this) *= a);
    }

    Matrix<T> operator^(const Matrix<T> &a)
    {
        return (Matrix<T>(*this) ^= a);
    }

    const std::vector<T> &operator[](int i) const
    {
        return a[i];
    }

    vector<T> &operator[](int i)
    {
        return a[i];
    }
};

int main()
{
    ll n;
    cin >> n;
    Matrix<mint> a(2);
    a[0][0] = a[0][1] = a[1][0] = 1;
    a ^= n;
    cout << a[0][0] - 1 << endl;
}
0