結果

問題 No.658 テトラナッチ数列 Hard
ユーザー T1610T1610
提出日時 2018-04-28 18:03:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 214 ms / 2,000 ms
コード長 1,762 bytes
コンパイル時間 1,453 ms
コンパイル使用メモリ 167,016 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-10 07:51:57
合計ジャッジ時間 3,293 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll MOD = 17;
double EPS = 1e-8;

template <int SZ>
struct Mat {
    array<array<ll, SZ>, SZ> d;
    const int n;
    Mat() : n(SZ) {
        array<ll, SZ> tmp;
        tmp.fill(0);
        d.fill(tmp);
    }
    Mat<SZ> operator * (const Mat<SZ>& mt) const {
        Mat<SZ> ret;
        rep(i, SZ) rep(j, SZ) {
            ll sum = 0LL;
            rep(k, SZ) {
                (ret.d[i][j] += (d[i][k] * mt.d[k][j]) % MOD) %= MOD;
            }
        }
        return ret;
    }
    Mat<SZ>& operator = (const Mat<SZ>& mt) {
        d = mt.d;
        return *this;
    }
};
template <int SZ> 
Mat<SZ> pow(Mat<SZ> mt, ll n) {
    Mat<SZ> ret;
    rep(i, SZ) ret.d[i][i] = 1LL;
    while(n > 0) {
        if(n&1LL) ret = ret * mt;
        mt = mt*mt;
        n >>= 1;
    }
    return ret;
}

int main() {
    int q;
    cin >> q;
    Mat<4> mt;
    rep(i, 4) mt.d[0][i] = 1;
    rep(i, 3) mt.d[i+1][i] = 1;
    rep(i, q) {
        ll n;
        cin >> n;
        auto tmp = pow(mt, n);
        // cout << "n : " << tmp.n << endl;
        // rep(i, tmp.n) {
        //     rep(j, tmp.n) {
        //         cout << " " << tmp.d[i][j];
        //     }
        //     cout << endl;
        // }        
        cout << tmp.d[3][3] << endl;
    }
    return 0;
}
0