結果

問題 No.658 テトラナッチ数列 Hard
ユーザー tottoripaper
提出日時 2018-03-16 22:17:41
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 335 ms / 2,000 ms
コード長 1,801 bytes
コンパイル時間 2,028 ms
コンパイル使用メモリ 174,388 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-21 14:44:57
合計ジャッジ時間 4,025 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

const ll MOD = 17;

template <typename T>
using Matrix = std::vector<std::vector<T>>;

template <typename T>
Matrix<T> mult(const Matrix<T>& m1, const Matrix<T>& m2){
    assert(m1.size() > 0);
    assert(m2.size() > 0);
    assert(m1[0].size() == m2.size());
    int n = m1.size(), m = m1[0].size(), l = m2[0].size();
        
    Matrix<T> res(n);
    for(int i=0;i<n;++i){
        res[i].resize(l);

        for(int j=0;j<l;++j){
            for(int k=0;k<m;++k){
                res[i][j] = (res[i][j] + m1[i][k] * m2[k][j]) % MOD;
            }
        }
    }

    return res;
}

template <typename T>
Matrix<T> expt(Matrix<T> A, ll n){
    int m = A.size();
    Matrix<T> res(m);
    for(int i=0;i<m;++i){
        res[i].resize(m);
        for(int j=0;j<m;++j){
            res[i][j] = i == j ? 1 : 0;
        }
    }
    
    while(n){
        if(n & 1){res = mult(res, A);}
        A = mult(A, A);
        n >>= 1;
    }
    return res;
}

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    int Q;
    std::cin >> Q;

    for(int i=0;i<Q;++i){
        ll N;
        std::cin >> N;

        Matrix<int> A(4);
        for(int i=0;i<3;++i){
            A[i].resize(4);
            for(int j=0;j<4;++j){
                A[i][j] = j == i + 1 ? 1 : 0;
            }
        }

        A[3].resize(4);
        for(int j=0;j<4;++j){
            A[3][j] = 1;
        }
        
        Matrix<int> B = expt(A, N - 1);
        int res = B[0][3];
        std::cout << res << std::endl;
    }
}
0