結果

問題 No.658 テトラナッチ数列 Hard
ユーザー ryoissyryoissy
提出日時 2018-03-02 22:36:51
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 336 ms / 2,000 ms
コード長 921 bytes
コンパイル時間 1,430 ms
コンパイル使用メモリ 150,600 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 23:34:11
合計ジャッジ時間 3,570 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 119 ms
4,380 KB
testcase_05 AC 138 ms
4,380 KB
testcase_06 AC 175 ms
4,380 KB
testcase_07 AC 188 ms
4,380 KB
testcase_08 AC 224 ms
4,376 KB
testcase_09 AC 336 ms
4,380 KB
testcase_10 AC 335 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define MOD 17
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> 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++){
				C[i][j]=(C[i][j]+A[i][k]*B[k][j]%MOD)%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&1LL)B=mul(B,A);
		A=mul(A,A);
		n>>=1LL;
	}
	return B;
}

int main(void){
	int q;
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		ll n;
		scanf("%lld",&n);
		if(n<=3LL){
			printf("0\n");
			continue;
		}
		mat A(4,vec(4));
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				A[i][j]=0;
			}
			if(i>0)A[i][i-1]=1;
		}
		for(int i=0;i<4;i++){
			A[0][i]=1;
		}
		A=pow(A,n-4LL);
		printf("%d\n",A[0][0]);
	}
	return 0;
}
0