結果

問題 No.658 テトラナッチ数列 Hard
ユーザー ldsybldsyb
提出日時 2018-03-03 15:45:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,362 ms / 2,000 ms
コード長 1,072 bytes
コンパイル時間 1,532 ms
コンパイル使用メモリ 172,812 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-06 22:04:50
合計ジャッジ時間 8,628 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 477 ms
4,376 KB
testcase_05 AC 551 ms
4,376 KB
testcase_06 AC 702 ms
4,376 KB
testcase_07 AC 758 ms
4,380 KB
testcase_08 AC 897 ms
4,376 KB
testcase_09 AC 1,362 ms
4,376 KB
testcase_10 AC 1,352 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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