結果

問題 No.657 テトラナッチ数列 Easy
ユーザー KKT89KKT89
提出日時 2020-10-20 03:35:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 119 ms / 2,000 ms
コード長 2,826 bytes
コンパイル時間 2,247 ms
コンパイル使用メモリ 207,148 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-28 13:34:30
合計ジャッジ時間 3,989 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 64 ms
4,376 KB
testcase_10 AC 82 ms
4,380 KB
testcase_11 AC 83 ms
4,380 KB
testcase_12 AC 101 ms
4,380 KB
testcase_13 AC 118 ms
4,376 KB
testcase_14 AC 119 ms
4,380 KB
testcase_15 AC 119 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
// constexpr ll mod=1e9+7;

constexpr ll mod=17;

template<class T>

struct Mat{
    vector<vector<T>> A;
    Mat(){}
    Mat(size_t n,size_t m):A(n,vector<T>(m,0)){}
    Mat(size_t n):A(n,vector<T>(n,0)){};
    size_t height() const{
        return A.size();
    }
    size_t width() const{
        return A[0].size();
    }
    inline const vector<T> &operator[](int k) const{
        return A.at(k);
    }
    inline vector<T> &operator[](int k){
        return A.at(k);
    }
    static Mat I(size_t n){
        Mat mat(n);
        for(int i=0;i<n;i++){
            mat[i][i]=1;
        }
        return mat;
    }
    Mat &operator+=(const Mat &B){
        size_t n=height(),m=width();
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                (*this)[i][j]=((*this)[i][j]+B[i][j])%mod;
            }
        }
        return (*this);
    }
    Mat &operator-=(const Mat &B){
        size_t n=height(),m=width();
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                (*this)[i][j]=((*this)[i][j]-B[i][j])%mod;
                if((*this)[i][j]<0)(*this)[i][j]+=mod;
            }
        }
        return (*this);
    }
    Mat &operator*=(const Mat &B){
        int n=height(),m=B.width(),p=width();
        assert(p==B.height());
        vector<vector<T>> C(n,vector<T>(m,0));
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                for(int k=0;k<p;k++){
                    C[i][j]=(C[i][j]+(*this)[i][k]*B[k][j])%mod;
                }
            }
        }
        A.swap(C);
        return (*this);
    }
    Mat &operator^=(ll k){
        Mat B=Mat::I(height());
        while(k){
            if(k%2)B*=(*this);
            (*this)*=(*this);
            k>>=1LL;
        }
        A.swap(B.A);
        return (*this);
    }
    Mat operator+(const Mat &B) const{
        return (Mat(*this)+=B);
    }
    Mat operator-(const Mat &B) const{
        return (Mat(*this)-=B);
    }
    Mat operator*(const Mat &B) const{
        return (Mat(*this)*=B);
    }
    Mat operator^(const Mat &B) const{
        return (Mat(*this)^=B);
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int q; cin >> q;
    while(q--){
        ll n; cin >> n;
        if(n<4){
            printf("0\n");
        }
        else if(n==4){
            printf("1\n");
        }
        else{
            Mat<ll> A(4,1),B(4);
            A[0][0]=1;
            B[0][0]=B[0][1]=B[0][2]=B[0][3]=B[1][0]=B[2][1]=B[3][2]=1;
            B^=(n-4);
            A=B*A;
            printf("%lld\n",A[0][0]);
        }
    }
}
0