結果
| 問題 |
No.658 テトラナッチ数列 Hard
|
| コンテスト | |
| ユーザー |
ama_nuko
|
| 提出日時 | 2018-06-29 16:39:20 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 600 ms / 2,000 ms |
| コード長 | 1,610 bytes |
| コンパイル時間 | 1,014 ms |
| コンパイル使用メモリ | 104,400 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-30 23:49:35 |
| 合計ジャッジ時間 | 4,333 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 8 |
ソースコード
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <utility>
#include <iomanip>
#define int long long int
#define rep(i, n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef pair<int, int> P;
const int INF = 1e15;
const int MOD = 17;
typedef vector<int> Vec;
typedef vector<Vec> Mat;
Mat multi(Mat a, Mat b){
Mat c(a.size(), Vec(b[0].size()));
rep(i, (int)a.size()){
rep(j, (int)b[0].size()){
rep(k, (int)b.size()){
c[i][j] = (c[i][j] + a[i][k] * b[k][j] % MOD) % MOD;
}
}
}
return c;
}
Vec multi(Mat a, Vec b){
Vec c(a.size());
rep(i, (int)a.size()){
rep(j, (int)b.size()){
c[i] = (c[i] + a[i][j] * b[j] % MOD) % MOD;
}
}
return c;
}
Mat powMat(Mat a, int n){
Mat b(a.size(), Vec(a[0].size()));;
rep(i, (int)a.size()){
rep(j, (int)a[0].size()){
if(i == j){
b[i][j] = 1;
}
}
}
while(n != 0){
if(n % 2 == 1){
b = multi(b, a);
}
n /= 2;
a = multi(a, a);
}
return b;
}
signed main(){
Mat x(4, Vec(4));
x[0][0] = x[0][1] = x[0][2] = x[0][3] = x[1][0] = x[2][1] = x[3][2] = 1;
Vec v = {1, 0, 0, 0};
int q;
cin >> q;
rep(i, q){
int n;
cin >> n;
if(n <= 4){
cout << v[4-n] << endl;
continue;
}
Vec ans = multi(powMat(x, n-4), v);
cout << ans[0] << endl;
}
return 0;
}
ama_nuko