結果

問題 No.658 テトラナッチ数列 Hard
ユーザー nebukuro09nebukuro09
提出日時 2018-03-03 00:37:50
言語 D
(dmd 2.106.1)
結果
RE  
実行時間 -
コード長 1,044 bytes
コンパイル時間 712 ms
コンパイル使用メモリ 100,016 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 18:56:55
合計ジャッジ時間 2,504 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

immutable long MOD = 17;

void main() {
    long[][] mat = [[1, 1, 1, 1],
                    [1, 0, 0, 0],
                    [0, 1, 0, 0],
                    [0, 0, 1, 0]];

    auto Q = readln.chomp.to!int;
    while (Q--) {
        auto N = readln.chomp.to!int;
        auto A = powmod(mat, N);
        A[3][3].writeln;
    }
}

long[][] mul(long[][] A, long[][] B) {
    auto n = A.length.to!int;
    auto ret = new long[][](n, n);
    foreach (i; 0..n)
        foreach (j; 0..n)
            foreach (k; 0..n)
                (ret[i][j] += A[i][k] * B[k][j]) %= MOD;
    return ret;
}

long[][] powmod(long[][] A, long x) {
    auto n = A.length.to!int;
    auto ret = new long[][](n, n);
    foreach (i; 0..n) ret[i][i] = 1;
    while (x) {
        if (x % 2) ret = mul(ret, A);
        A = mul(A, A);
        x /= 2;
    }
    return ret;
}
0