結果

問題 No.658 テトラナッチ数列 Hard
ユーザー くれちーくれちー
提出日時 2018-03-02 23:16:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 260 ms / 2,000 ms
コード長 1,055 bytes
コンパイル時間 741 ms
コンパイル使用メモリ 73,840 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-04 06:57:11
合計ジャッジ時間 2,708 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 102 ms
4,380 KB
testcase_05 AC 114 ms
4,376 KB
testcase_06 AC 142 ms
4,380 KB
testcase_07 AC 152 ms
4,376 KB
testcase_08 AC 177 ms
4,376 KB
testcase_09 AC 260 ms
4,376 KB
testcase_10 AC 259 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

typedef std::vector<std::vector<int>> matrix;
const int mod = 17;

matrix multiply(matrix &l, matrix &r) {
    matrix ret(4, std::vector<int>(4));
    rep (i, 4) rep (k, 4) rep (j, 4) (ret[i][j] += l[i][k] * r[k][j]) %= mod;
    return ret;
}

matrix pow(matrix v, long long n) {
    matrix ret(4, std::vector<int>(4));
    rep (i, 4) ret[i][i] = 1;
    while (n > 0) {
        if (n & 1) ret = multiply(ret, v);
        v = multiply(v, v);
        n >>= 1;
    }
    return ret;
}

int main() {
    matrix mat(4, std::vector<int>(4));
    mat[0][0] = 1; mat[0][1] = 1; mat[0][2] = 1; mat[0][3] = 1;
    mat[1][0] = 1; mat[1][1] = 0; mat[1][2] = 0; mat[1][3] = 0;
    mat[2][0] = 0; mat[2][1] = 1; mat[2][2] = 0; mat[2][3] = 0;
    mat[3][0] = 0; mat[3][1] = 0; mat[3][2] = 1; mat[3][3] = 0;
    int q;
    std::cin >> q;
    rep (i, q) {
        long long n;
        std::cin >> n;
        int ans = pow(mat, n - 1)[3][0];
        std::cout << ans << std::endl;
    }
}
0