結果

問題 No.657 テトラナッチ数列 Easy
ユーザー kappybarkappybar
提出日時 2020-04-26 18:01:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 118 ms / 2,000 ms
コード長 1,321 bytes
コンパイル時間 1,840 ms
コンパイル使用メモリ 175,316 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-28 20:18:16
合計ジャッジ時間 3,456 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using ll =  long long ;
using P = pair<int,int> ;
const int INF = 1e9;
const int MOD = 17;
using vec = vector<ll> ;
using mat =  vector<vec>;

mat mul(mat &A, mat &B) {
        mat C(A.size(),vec(B[0].size()));
        for(int i=0;i<A.size();i++){
                for(int k=0;k<B.size();k++){
                        for(int j=0;j<B[0].size();j++){
                                C[i][j] = (C[i][j] + A[i][k]*B[k][j]) % MOD;
                        }
                }
        }
        return C;
}

mat pow(mat A,ll n){
        mat B(A.size(), vec(A.size()));
        for(int i=0;i<A.size();i++) B[i][i] = 1;
        while(n > 0){
                if(n & 1) B = mul(B,A);
                A = mul(A,A);
                n >>= 1;
        }
        return B;
}

int main(){
    int q;
    cin >> q;
    vec four = {1,0,0,0};
    mat A = {{1,1,1,1},
             {1,0,0,0},
             {0,1,0,0},
             {0,0,1,0}};
    while(q--){
        ll n;
        cin >> n;
        if(n<=4){
            cout << four[4-n] << endl;
            continue;
        }
        mat B = pow(A,n-4);
        ll ans = 0;
        rep(i,4){
            ans = (ans + (B[0][i] * four[i])%MOD)%MOD;
        }
        cout << ans << endl;
    }
    return 0;
}
0