結果

問題 No.658 テトラナッチ数列 Hard
ユーザー nmnmnmnmnmnmnmnmnmnmnmnmnmnm
提出日時 2018-03-03 00:16:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 670 ms / 2,000 ms
コード長 2,373 bytes
コンパイル時間 1,603 ms
コンパイル使用メモリ 104,648 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-05 04:13:16
合計ジャッジ時間 6,065 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 8 ms
4,380 KB
testcase_04 AC 556 ms
4,380 KB
testcase_05 AC 567 ms
4,380 KB
testcase_06 AC 587 ms
4,376 KB
testcase_07 AC 596 ms
4,384 KB
testcase_08 AC 610 ms
4,380 KB
testcase_09 AC 670 ms
4,380 KB
testcase_10 AC 670 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iomanip>
using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define per(i,a,b) for(ll i=(b-1);i>=(a);--i)
#define clr(a, b) memset((a), (b) ,sizeof(a))
#define ctos(c) string(1,c)
#define print(x) cout<<#x<<" = "<<x<<endl;

#define MOD 17

vector<vector<long long> > mul(vector<vector<long long> > v1, vector<vector<long long> > v2){
    vector<vector<long long> > ret(v1.size(),vector<long long>(v2[0].size(),0));
    for(int i=0;i<v1.size();i++) {
        for(int j=0;j<v2[0].size();j++) {
            for(int k=0;k<v1.size();k++) {
                ret[i][j]+=v1[i][k]*v2[k][j];
                ret[i][j]%=MOD;
            }
        }
    }
    return ret;
}

vector<vector<long long> > powmatrix(vector<vector<long long> > vv, vector<vector<long long> > v, long long n){
    vector<vector<vector<long long> > > vvv;
    vvv.push_back(vv);
    for(long long i = 0; i < 64; i++){
        vvv.push_back(mul(vvv[i],vvv[i]));
    }
    vector<vector<long long> > v1(vv.size(), vector<long long>(vv.size(), 0));
    for(int i = 0; i < vv.size(); i++)v1[i][i] = 1;
    for(int i = 0; i < 64; i++){
        if((n>>i)&1){
            v1 = mul(v1,vvv[i]);
        }
    }
    v = mul(v1,v);
    return v;
}

int main() {
	vector<vector<long long> > v(4, vector<long long>(1, 0));
	v[0][0] = 1;
	v[1][0] = 0;
	v[2][0] = 0;
	v[3][0] = 0;
	vector<vector<long long> > vv(4, vector<long long>(4, 0));
	vv[0][0] = 1; vv[0][1] = 1; vv[0][2] = 1; vv[0][3] = 1;
	vv[1][0] = 1; vv[1][1] = 0; vv[1][2] = 0; vv[1][3] = 0;
	vv[2][0] = 0; vv[2][1] = 1; vv[2][2] = 0; vv[2][3] = 0;
	vv[3][0] = 0; vv[3][1] = 0; vv[3][2] = 1; vv[3][3] = 0;
	ll n;
	cin>>n;
	rep(i,0,n){
		ll a;
		cin>>a;
		if(a<4)cout << 0 << endl;
		else if(a==4)cout << 1 << endl;
		else{
			a-=4;
			vector<vector<long long> > va = powmatrix(vv,v,a);
			cout << va[0][0] << endl;
		}
	}
	return 0;
}
0