結果

問題 No.658 テトラナッチ数列 Hard
ユーザー hanbei_dayohanbei_dayo
提出日時 2020-08-31 22:22:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 395 ms / 2,000 ms
コード長 1,391 bytes
コンパイル時間 1,006 ms
コンパイル使用メモリ 92,084 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 11:21:28
合計ジャッジ時間 3,344 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 149 ms
5,376 KB
testcase_05 AC 169 ms
5,376 KB
testcase_06 AC 210 ms
5,376 KB
testcase_07 AC 225 ms
5,376 KB
testcase_08 AC 261 ms
5,376 KB
testcase_09 AC 391 ms
5,376 KB
testcase_10 AC 395 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<functional>
#include<math.h>
using namespace std;
//#define N (1000000000+7)
//#define N (998244353)
#define N 17
#define INF 1e16
typedef long long ll;
typedef pair<int,int> P;
typedef vector<ll> vec;
typedef vector<vec> mat;

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++){
                ll t = (A[i][k]*B[k][j])%N;
                C[i][j] = (C[i][j]+t)%N;
                C[i][j] = (C[i][j]+N)%N;
            }
        }
    }
    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]=1LL;
    while(n>0){
        if(n&1)B = mul(B,A);
        A = mul(A,A);
        n>>=1;
    }
    return B;
}

int main(void){
    ll Q;
    cin>>Q;
    mat A(4,vec(4));
    for(int i=0;i<4;i++){
        A[0][i]=1;
    }
    A[1][0]=1;
    A[2][1]=1;
    A[3][2]=1;
    while(Q--){
        ll n;
        cin>>n;
        if(n<=3){
            cout<<0<<endl;
            continue;
        }
        if(n==4){
            cout<<1<<endl;
            return 0;
        }
        mat Ans(4,vec(4));
        Ans = Pow(A,n-4);
        cout<<Ans[0][0]<<endl;
    }
}
 
0