結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 16 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 118 ms
4,376 KB
testcase_10 AC 153 ms
4,376 KB
testcase_11 AC 152 ms
4,376 KB
testcase_12 AC 187 ms
4,376 KB
testcase_13 AC 222 ms
4,376 KB
testcase_14 AC 221 ms
4,380 KB
testcase_15 AC 223 ms
4,376 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