結果

問題 No.657 テトラナッチ数列 Easy
ユーザー ldsyb
提出日時 2018-03-03 15:45:46
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 477 ms / 2,000 ms
コード長 1,072 bytes
コンパイル時間 1,876 ms
コンパイル使用メモリ 174,632 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-24 16:15:03
合計ジャッジ時間 5,432 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using vec = vector<int64_t>;
using mat = vector<vec>;

mat mul_mod(mat &a, mat &b, int64_t mod) {
	mat c(a.size(), vec(b[0].size(), 0));
	for (int k = 0; k < a[0].size(); k++) {
		for (int i = 0; i < a.size(); i++) {
			for (int j = 0; j < b[0].size(); j++) {
				(c[i][j] += a[i][k] * b[k][j] % mod) %= mod;
			}
		}
	}
	return c;
}

mat pow_mod(mat a, int64_t n, int64_t mod) {
	mat r(a.size(), vec(a.size(), 0));
	for (int i = 0; i < a.size(); i++) {
		r[i][i] = 1;
	}
	for (; n; n >>= 1) {
		if (n & 1) {
			r = mul_mod(r, a, mod);
		}
		a = mul_mod(a, a, mod);
	}
	return r;
}

int main() {
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int64_t n;
		cin >> n;
		if (n <= 4) {
			int a[] = {0, 0, 0, 1};
			cout << a[n - 1] << endl;
			continue;
		}
		mat a(4, vec(4, 0));
		for (int j = 0; j < 4; j++) {
			a[0][j] = 1;
		}
		for (int j = 0; j < 3; j++) {
			a[j + 1][j] = 1;
		}
		a = pow_mod(a, n - 4, 17);
		mat x(4, vec(1, 0));
		x[0][0] = 1;
		a = mul_mod(a, x, 17);
		cout << a[0][0] << endl;
	}
	return 0;
}
0