結果

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

テストケース

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

ソースコード

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!long;
        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