結果

問題 No.2156 ぞい文字列
ユーザー nono00nono00
提出日時 2022-12-10 08:35:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,413 bytes
コンパイル時間 855 ms
コンパイル使用メモリ 82,960 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-22 23:39:41
合計ジャッジ時間 1,437 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

constexpr long long mod = 998244353;

vector<vector<long long>> mul(const vector<vector<long long>> &x, const vector<vector<long long>> &y) {
    int n, m, l;
    n = x.size();
    m = y.size();
    l = y[0].size();

    vector<vector<long long>> res(n, vector<long long>(l, 0LL));

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < l; k++) {
                res[i][k] += (x[i][j] * y[j][k]) % mod;
                res[i][k] %= mod;
            }
        }
    }

    return res;
}

vector<vector<long long>> pow(const vector<vector<long long>> &v, long long p) {
    int sz = v.size();
    vector<vector<long long>> res(sz, vector<long long>(sz, 0));
    vector<vector<long long>> x = v;
    for (int i = 0; i < sz; i++) {
        res[i][i] = 1LL;
    }

    while (p > 0) {
        if (p & 1) {
            res = mul(res, x);
        }
        p >>= 1;
        x = mul(x, x);
    }

    return res;
}

int main() {
    long long n;
    cin >> n;
    vector<vector<long long>> init = {
        {0},
        {1},
        {0},
        {0}
    };
    vector<vector<long long>> p = {
        {0, 1, 0, 0},
        {1, 1, 0, 0},
        {1, 0, 0, 0},
        {0, 1, 0, 0}
    };

    vector<vector<long long>> ans = mul(pow(p, n - 1), init);

    cout << ((ans[0][0] + ans[1][0] - 1) % mod) << endl;
}
0