結果
| 問題 |
No.658 テトラナッチ数列 Hard
|
| コンテスト | |
| ユーザー |
peroon
|
| 提出日時 | 2019-04-26 20:57:55 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 590 ms / 2,000 ms |
| コード長 | 1,659 bytes |
| コンパイル時間 | 1,868 ms |
| コンパイル使用メモリ | 176,572 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-11-25 00:52:47 |
| 合計ジャッジ時間 | 5,315 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 8 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using VV = vector<vector<ll> >;
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")
const ll mod = 17;
const ll inf = 1e18;
template < typename T >
void vprint(T &V){
for(auto v : V){
cout << v << " ";
}
cout << endl;
}
// Matrix Library
VV mat_mul(const VV& x, const VV& y){
int a = x.size();
int b = x[0].size();
int c = y[0].size();
VV z(a, vector<ll>(c, 0));
FOR(i, 0, a){
FOR(j, 0, c){
FOR(k, 0, b){
z[i][j] += x[i][k] * y[k][j];
z[i][j] %= mod; // 必要であれば
}
}
}
return z;
}
VV mat_pow(const VV& x, ll k){
int n = x.size();
VV y(n, vector<ll>(n, 0));
FOR(i, 0, n){
y[i][i] = 1;
}
VV z = x;
if(k==0) return y;
if(k==1) return z;
if(k%2==0){
return mat_pow(mat_mul(x, x), k/2);
}else{
return mat_mul(x, mat_pow(x, k-1));
}
}
void print_mat(VV x){
ll H = x.size();
FOR(i, 0, H){
vprint(x[i]);
}
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
// input
ll Q;
cin >> Q;
VV A ={
{1, 1, 1, 1},
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
};
FOR(i, 0, Q){
ll n;
cin >> n;
auto B = mat_pow(A, n);
ll ans = B[3][3];
p(ans);
}
return 0;
}
peroon