結果

問題 No.658 テトラナッチ数列 Hard
ユーザー komori3komori3
提出日時 2018-07-05 20:41:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 535 ms / 2,000 ms
コード長 5,135 bytes
コンパイル時間 1,566 ms
コンパイル使用メモリ 171,260 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 02:41:17
合計ジャッジ時間 4,620 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 193 ms
6,944 KB
testcase_05 AC 222 ms
6,944 KB
testcase_06 AC 285 ms
6,944 KB
testcase_07 AC 302 ms
6,944 KB
testcase_08 AC 359 ms
6,944 KB
testcase_09 AC 535 ms
6,944 KB
testcase_10 AC 535 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
#include <random>
#include <unordered_map>
#include <unordered_set>

using namespace std;

//呪文
#define DUMPOUT cerr
#define dump(...) DUMPOUT<<"  ";DUMPOUT<<#__VA_ARGS__<<" :["<<__LINE__<<":"<<__FUNCTION__<<"]"<<endl;DUMPOUT<<"    ";dump_func(__VA_ARGS__)

typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; typedef pair<string, string> pss;
template <typename _KTy, typename _Ty> ostream& operator << (ostream& ostr, const pair<_KTy, _Ty>& m) { ostr << "{" << m.first << ", " << m.second << "}"; return ostr; }
template <typename _KTy, typename _Ty> ostream& operator << (ostream& ostr, const map<_KTy, _Ty>& m) { if (m.empty()) { ostr << "{ }"; return ostr; } ostr << "{" << *m.begin(); for (auto itr = ++m.begin(); itr != m.end(); itr++) { ostr << ", " << *itr; } ostr << "}"; return ostr; }
template <typename _KTy, typename _Ty> ostream& operator << (ostream& ostr, const unordered_map<_KTy, _Ty>& m) { if (m.empty()) { ostr << "{ }"; return ostr; } ostr << "{" << *m.begin(); for (auto itr = ++m.begin(); itr != m.end(); itr++) { ostr << ", " << *itr; } ostr << "}"; return ostr; }
template <typename _Ty> ostream& operator << (ostream& ostr, const vector<_Ty>& v) { if (v.empty()) { ostr << "{ }"; return ostr; } ostr << "{" << v.front(); for (auto itr = ++v.begin(); itr != v.end(); itr++) { ostr << ", " << *itr; }	ostr << "}"; return ostr; }
template <typename _Ty> ostream& operator << (ostream& ostr, const set<_Ty>& s) { if (s.empty()) { ostr << "{ }"; return ostr; } ostr << "{" << *(s.begin()); for (auto itr = ++s.begin(); itr != s.end(); itr++) { ostr << ", " << *itr; }	ostr << "}"; return ostr; }
template <typename _Ty> ostream& operator << (ostream& ostr, const unordered_set<_Ty>& s) { if (s.empty()) { ostr << "{ }"; return ostr; } ostr << "{" << *(s.begin()); for (auto itr = ++s.begin(); itr != s.end(); itr++) { ostr << ", " << *itr; }	ostr << "}"; return ostr; }
template <typename _Ty> ostream& operator << (ostream& ostr, const stack<_Ty>& s) { if (s.empty()) { ostr << "{ }"; return ostr; } stack<_Ty> t(s); ostr << "{" << t.top(); t.pop(); while (!t.empty()) { ostr << ", " << t.top(); t.pop(); } ostr << "}";	return ostr; }
template <typename _Ty> ostream& operator << (ostream& ostr, const list<_Ty>& l) { if (l.empty()) { ostr << "{ }"; return ostr; } ostr << "{" << l.front(); for (auto itr = ++l.begin(); itr != l.end(); ++itr) { ostr << ", " << *itr; } ostr << "}"; return ostr; }
template <typename _KTy, typename _Ty> istream& operator >> (istream& istr, pair<_KTy, _Ty>& m) { istr >> m.first >> m.second; return istr; }
template <typename _Ty> istream& operator >> (istream& istr, vector<_Ty>& v) { for (size_t i = 0; i < v.size(); i++) istr >> v[i]; return istr; }
namespace aux { // print tuple
	template<typename Ty, unsigned N, unsigned L> struct tp { static void print(ostream& os, const Ty& v) { os << get<N>(v) << ", "; tp<Ty, N + 1, L>::print(os, v); } };
	template<typename Ty, unsigned N> struct tp<Ty, N, N> { static void print(ostream& os, const Ty& value) { os << get<N>(value); } };
}
template<typename... Tys> ostream& operator<<(ostream& os, const tuple<Tys...>& t) { os << "{"; aux::tp<tuple<Tys...>, 0, sizeof...(Tys)-1>::print(os, t); os << "}"; return os; }

template<typename A, size_t N, typename T> inline void Fill(A(&array)[N], const T &val) { std::fill((T*)array, (T*)(array + N), val); }

void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail> void dump_func(Head&& head, Tail&&... tail) { DUMPOUT << head; if (sizeof...(Tail) == 0) { DUMPOUT << " "; } else { DUMPOUT << ", "; } dump_func(std::move(tail)...); }

#define PI 3.14159265358979323846
#define EPS 1e-8
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define fake false



struct Mat4x4 {
	vector<vector<ll>> m_a;
	Mat4x4() {
		m_a.resize(4, vector<ll>(4, 0));
		for (int i = 0; i < 4; i++) m_a[i][i] = 1;
	}
	Mat4x4(const vector<vector<ll>>& a) : m_a(a) {}
};

Mat4x4 operator*(const Mat4x4& m1, const Mat4x4& m2) {
	vector<vector<ll>> m(4, vector<ll>(4, 0));
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			for (int k = 0; k < 4; k++) {
				m[i][j] += m1.m_a[i][k] * m2.m_a[k][j];
			}
		}
	}
	return Mat4x4(m);
}

Mat4x4 operator%(const Mat4x4& m, const ll MOD) {
	Mat4x4 res(m);
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			res.m_a[i][j] %= MOD;
		}
	}
	return res;
}

// n^e mod m
Mat4x4 modpow(Mat4x4 n, ll e, ll m) {

	Mat4x4 result = Mat4x4();

	while (e > 0) {
		if (e & 1) result = (result * n) % m;
		e >>= 1;
		n = (n * n) % m;
	}

	return result;
}

int main() {

	//cin.tie(0);
	//ios::sync_with_stdio(false);
	
	vector<vector<ll>> m({
		{1, 1, 1, 1},
		{1, 0, 0, 0},
		{0, 1, 0, 0},
		{0, 0, 1, 0}
	});
	Mat4x4 F(m);

	int Q;
	cin >> Q;
	REP(i, Q) {
		ll n;
		cin >> n;
		
		Mat4x4 M = modpow(F, n - 1, 17LL);
		cout << M.m_a[3][0] % 17 << endl;
	}

	return 0;
}
0