結果

問題 No.658 テトラナッチ数列 Hard
ユーザー ミドリムシミドリムシ
提出日時 2018-03-02 23:56:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 1,316 bytes
コンパイル時間 586 ms
コンパイル使用メモリ 70,164 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-05 02:25:28
合計ジャッジ時間 1,692 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 32 ms
4,376 KB
testcase_05 AC 35 ms
4,376 KB
testcase_06 AC 40 ms
4,380 KB
testcase_07 AC 42 ms
4,380 KB
testcase_08 AC 47 ms
4,376 KB
testcase_09 AC 63 ms
4,376 KB
testcase_10 AC 64 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct matrix{
    int space[4][4];
    
    matrix operator *(const matrix &another) const{
        matrix ans;
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                ans.space[i][j] = 0;
                for(int k = 0; k < 4; k++){
                    ans.space[i][j] += space[i][k] * another.space[k][j];
                }
                ans.space[i][j] %= 17;
            }
        }
        return ans;
    }
    
    void output(){
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                cout << space[i][j] << " ";
            }
            cout << endl;
        }
        cout << endl;
    }
};

const matrix A = {{{1, 1, 1, 1}, {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}}};
const matrix E = {{{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}};

matrix power(long exponent){
    if(exponent % 2){
        return power(exponent - 1) * A;
    }else if(exponent){
        matrix root_ans = power(exponent / 2);
        return root_ans * root_ans;
    }else{
        return E;
    }
}

int main(){
    int Q;
    cin >> Q;
    for(int i = 0; i < Q; i++){
        long num;
        cin >> num;
        cout << power(num - 1).space[3][0] << endl;
    }
}
0