結果

問題 No.658 テトラナッチ数列 Hard
ユーザー ama_nukoama_nuko
提出日時 2018-06-29 16:39:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 616 ms / 2,000 ms
コード長 1,610 bytes
コンパイル時間 1,113 ms
コンパイル使用メモリ 99,604 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 15:12:32
合計ジャッジ時間 4,643 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 224 ms
4,380 KB
testcase_05 AC 261 ms
4,376 KB
testcase_06 AC 325 ms
4,380 KB
testcase_07 AC 349 ms
4,376 KB
testcase_08 AC 412 ms
4,380 KB
testcase_09 AC 613 ms
4,376 KB
testcase_10 AC 616 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0