結果

問題 No.658 テトラナッチ数列 Hard
ユーザー ryoissyryoissy
提出日時 2018-03-02 22:36:51
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 324 ms / 2,000 ms
コード長 921 bytes
コンパイル時間 1,500 ms
コンパイル使用メモリ 164,836 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-13 04:06:47
合計ジャッジ時間 3,733 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 8
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:36:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   36 |         scanf("%d",&q);
      |         ~~~~~^~~~~~~~~
main.cpp:39:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   39 |                 scanf("%lld",&n);
      |                 ~~~~~^~~~~~~~~~~

ソースコード

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