結果

問題 No.658 テトラナッチ数列 Hard
ユーザー tottoripapertottoripaper
提出日時 2018-03-16 22:17:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 327 ms / 2,000 ms
コード長 1,801 bytes
コンパイル時間 1,821 ms
コンパイル使用メモリ 171,772 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-23 12:01:51
合計ジャッジ時間 4,054 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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