結果

問題 No.2156 ぞい文字列
ユーザー wanuiwanui
提出日時 2022-12-09 22:29:42
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 3,358 bytes
コンパイル時間 2,481 ms
コンパイル使用メモリ 204,552 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-22 22:36:33
合計ジャッジ時間 2,533 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18;
void print0(){}; template<typename H,typename... T> void print0(H h,T... t){cout<<h;print0(t...);}
void print(){print0("\n");}; template<typename H,typename... T>void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);}
void perr0(){}; template<typename H,typename... T> void perr0(H h,T... t){cerr<<h;perr0(t...);}
void perr(){perr0("\n");}; template<typename H,typename... T>void perr(H h,T... t){perr0(h);if(sizeof...(T)>0)perr0(" ");perr(t...);}
void ioinit() { cout<<fixed<<setprecision(15); cerr<<fixed<<setprecision(6); ios_base::sync_with_stdio(0); cin.tie(0); }
#define debug1(a) { cerr<<#a<<":"<<a<<endl; }
#define debug2(a,b) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<endl; }
#define debug3(a,b,c) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<" "<<#c<<":"<<c<<endl; }
#define debug4(a,b,c,d) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<" "<<#c<<":"<<c<<" "<<#d<<":"<<d<<endl; }
// clang-format on
const ll MODULO = 998244353;
using T = ll;
class matrix {
   public:
    ll row;
    ll col;
    vector<vector<T>> mat;
    matrix(ll r, ll c) {
        row = r;
        col = c;
        mat = vector<vector<T>>(r, vector<T>(c));
    }
    void set(ll r, ll c, T v) {
        mat[r][c] = v;
    }
    void setvec(vector<vector<T>>& v) {
        mat = v;
    }
    T get(ll r, ll c) {
        return mat[r][c];
    }
    matrix add(matrix& rhs) {
        auto result = matrix(row, col);
        for (ll i = 0; i < row; i++) {
            for (ll j = 0; j < col; j++) {
                result.mat[i][j] = (mat[i][j] + rhs.mat[i][j]) % MODULO;
            }
        }
        return result;
    }
    matrix mult(matrix& rhs) {
        /*
             S行*T列  * T行*U列  =>  S行*U列
            [ 1 2 3    [ 10      [1*10+2*30+3*50    [220
              4 5 6] *   30   =   4*10+5*30+6*50] =  490 ]
                         50 ]
            O(STU)  とくに正方行列なら O(S^3)
        */
        ll s = row;
        ll t = col;
        ll u = rhs.col;
        assert(rhs.row == t);
        auto result = matrix(s, u);
        for (ll i = 0; i < s; i++) {
            for (ll j = 0; j < u; j++) {
                for (ll k = 0; k < t; k++) {
                    result.mat[i][j] = (result.mat[i][j] + mat[i][k] * rhs.mat[k][j]) % MODULO;
                }
            }
        }
        return result;
    }
    matrix matpow(ll n) {
        // O(S^3 logN)
        assert(row == col);
        if (n == 0) {
            return unit(row);
        }
        auto half = matpow(n / 2);
        auto result = half.mult(half);
        if (n % 2) {
            result = result.mult(*this);
        }
        return result;
    }
    matrix unit(ll n) {
        auto u = matrix(n, n);
        for (ll i = 0; i < n; i++) {
            u.set(i, i, 1);
        }
        return u;
    }
};

int main() {
    ioinit();
    ll N;
    cin >> N;
    vector<ll> tbl = {0, 0, 1, 2};
    if (N < tbl.size()) {
        print(tbl[N]);
        return 0;
    }
    auto mat = matrix(2, 2);
    mat.set(0, 0, 1);
    mat.set(0, 1, 1);
    mat.set(1, 0, 1);
    mat.set(1, 1, 0);
    auto fib = mat.matpow(N);

    ll ans = fib.get(0, 0) - 1;
    if (ans < 0) ans += MODULO;
    print(ans);
    return 0;
}
0