結果

問題 No.2156 ぞい文字列
ユーザー sahiyasahiya
提出日時 2022-12-09 23:14:55
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 6,170 bytes
コンパイル時間 1,734 ms
コンパイル使用メモリ 139,504 KB
最終ジャッジ日時 2024-04-22 23:02:55
合計ジャッジ時間 2,060 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/string:43,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/bitset:52,
                 from main.cpp:5:
/home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/bits/allocator.h: In destructor 'std::_Vector_base<std::vector<long long int, std::allocator<long long int> >, std::allocator<std::vector<long long int, std::allocator<long long int> > > >::_Vector_impl::~_Vector_impl()':
/home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/bits/allocator.h:184:7: error: inlining failed in call to 'always_inline' 'std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = std::vector<long long int, std::allocator<long long int> >]': target specific option mismatch
  184 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/vector:66,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/functional:64,
                 from main.cpp:11:
/home/linuxbrew/.linuxbrew/Cellar/gcc/13.2.0/include/c++/13/bits/stl_vector.h:133:14: note: called from here
  133 |       struct _Vector_impl
      |              ^~~~~~~~~~~~

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define ALL(x) (x).begin(), (x).end()
#define PC(x) __builtin_popcount(x)
#define PCL(x) __builtin_popcountll(x)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <class T>
bool chmax(T& a, const T& b)
{
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
bool chmin(T& a, const T& b)
{
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
const double PI = 3.14159265358979323846;
const double PI2 = PI * 2.0;
const double EPS = 1E-09;
const ll MOD = 998244353;
const ll INFL = 1E18;
const int INFI = 1E09;
const int MAX_N = 2E+05;
struct edge {
    int to, cost, id;
};

ll di[4] = { 0, -1, 0, 1 }, dj[4] = { 1, 0, -1, 0 };

ll N;

template <typename T>
struct mat {
    // 行列m
    vector<vector<T>> m;
    // コンストラクタ:第1引数:行数、第2引数:列数、第3引数:初期値
    mat()
        : m(vector<vector<T>>(static_cast<T>(0)))
    {
    }
    mat(int h, int w)
        : m(vector<vector<T>>(h, vector<T>(w, static_cast<T>(0))))
    {
    }
    mat(int h, int w, T d)
        : m(vector<vector<T>>(h, vector<T>(w, d)))
    {
    }
    //単位行列を返すコンストラクタ:第1引数:n*n正方行列のn
    mat(int n)
        : m(vector<vector<T>>(n, vector<T>(n)))
    {
        for (int i = 0; i < n; i++) {

            m[i][i] = static_cast<T>(1);
        }
    }
    // 添字演算子
    vector<T> operator[](const int i) const { return m[i]; } //読み取り
    vector<T>& operator[](const int i) { return m[i]; } //書き込み
    // 行数・列数
    int nrow = m.size(); //行数
    int ncol = m[0].size(); //列数
    // 行列同士の演算
    mat& operator=(const mat& a)
    {
        m.resize(a.nrow);
        for (int i = 0; i < a.nrow; i++) {
            m[i].resize(a.ncol);
        }
        for (int i = 0; i < a.nrow; i++) {
            for (int j = 0; j < a.ncol; j++) {
                m[i][j] = a[i][j];
            }
        }
        nrow = a.nrow;
        ncol = a.ncol;
        return *this;
    }
    mat& operator+=(const mat& a)
    {
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < ncol; j++) {
                m[i][j] += a[i][j];
            }
        }
        return *this;
    }
    mat& operator-=(const mat& a)
    {
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < ncol; j++) {
                m[i][j] -= a[i][j];
            }
        }
        return *this;
    }

    mat& operator*=(const mat& a)
    {
        mat<T> m2(nrow, a.ncol, static_cast<T>(0));
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < a.ncol; j++) {
                for (int k = 0; k < nrow; k++) {
                    m2[i][j] += m[i][k] * a[k][j] % MOD;
                }
            }
        }
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < a.ncol; j++) {
                m[i][j] = m2[i][j];
            }
        }
        ncol = a.ncol;
        return *this;
    }

    mat operator+(const mat& a) const { return static_cast<mat>(*this) += a; }
    mat operator-(const mat& a) const { return static_cast<mat>(*this) -= a; }
    mat operator*(const mat& a) const { return static_cast<mat>(*this) *= a; }
    bool operator==(const mat& a)
    {
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < ncol; j++) {
                if (m[i][j] != a[i][j])
                    return false;
            }
        }
        return true;
    }

    // 行列とスカラの演算
    mat& operator+=(const T& a)
    {
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < ncol; j++) {
                m[i][j] += a;
            }
        }
        return *this;
    }
    mat& operator-=(const T& a)
    {
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < ncol; j++) {
                m[i][j] -= a;
            }
        }
        return *this;
    }
    mat& operator*=(const T& a)
    {
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < ncol; j++) {
                m[i][j] *= a;
            }
        }
        return *this;
    }
    mat& operator/=(const T& a)
    {
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < ncol; j++) {
                m[i][j] /= a;
            }
        }
        return *this;
    }
    mat operator+(const T& a) const { return static_cast<mat>(*this) += a; }
    mat operator-(const T& a) const { return static_cast<mat>(*this) -= a; }
    mat operator*(const T& a) const { return static_cast<mat>(*this) *= a; }
    mat operator/(const T& a) const { return static_cast<mat>(*this) /= a; }

    // 標準出力
    void show()
    {
        for (int i = 0; i < nrow; i++) {
            for (int j = 0; j < ncol; j++) {
                cout << m[i][j] << " ";
            }
            cout << "\n";
        }
        return;
    }
};

template <typename T>
mat<T> powmat(mat<T> x, ll a)
{
    mat<T> ans(x.nrow);
    while (a > 0) {
        if (a & 1) {
            ans = ans * x;
        }
        x = x * x;
        a >>= 1;
    }
    return ans;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> N;

    mat<ll> dp(2, 1, 0);
    dp[0][0] = 1;
    mat<ll> m(2, 2, 1);
    m[1][1] = 0;
    m = powmat(m, N - 1);
    dp = m * dp;

    // for (int i = 0; i < N; i++) {
    //     for (int j = 0; j < N; j++) {
    //         cout << "i = " << i << ", j = " << j << ", dp = " << dp[i][j] << "\n";
    //     }
    // }

    cout << (dp[0][0] + dp[1][0] - 1 + MOD) % MOD << "\n";

    return 0;
}
0