結果

問題 No.1704 Many Bus Stops (easy)
ユーザー kokatsukokatsu
提出日時 2021-11-22 22:15:23
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 415 ms / 2,000 ms
コード長 1,259 bytes
コンパイル時間 2,083 ms
コンパイル使用メモリ 203,528 KB
実行使用メモリ 4,436 KB
最終ジャッジ日時 2023-09-04 15:07:15
合計ジャッジ時間 15,277 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 405 ms
4,396 KB
testcase_02 AC 136 ms
4,384 KB
testcase_03 AC 137 ms
4,384 KB
testcase_04 AC 137 ms
4,388 KB
testcase_05 AC 136 ms
4,384 KB
testcase_06 AC 136 ms
4,420 KB
testcase_07 AC 137 ms
4,388 KB
testcase_08 AC 136 ms
4,400 KB
testcase_09 AC 136 ms
4,388 KB
testcase_10 AC 136 ms
4,384 KB
testcase_11 AC 136 ms
4,384 KB
testcase_12 AC 136 ms
4,380 KB
testcase_13 AC 136 ms
4,396 KB
testcase_14 AC 135 ms
4,384 KB
testcase_15 AC 137 ms
4,392 KB
testcase_16 AC 135 ms
4,380 KB
testcase_17 AC 136 ms
4,388 KB
testcase_18 AC 135 ms
4,392 KB
testcase_19 AC 135 ms
4,380 KB
testcase_20 AC 137 ms
4,380 KB
testcase_21 AC 136 ms
4,384 KB
testcase_22 AC 414 ms
4,380 KB
testcase_23 AC 415 ms
4,384 KB
testcase_24 AC 412 ms
4,384 KB
testcase_25 AC 412 ms
4,416 KB
testcase_26 AC 411 ms
4,380 KB
testcase_27 AC 412 ms
4,400 KB
testcase_28 AC 412 ms
4,384 KB
testcase_29 AC 413 ms
4,384 KB
testcase_30 AC 412 ms
4,384 KB
testcase_31 AC 414 ms
4,384 KB
testcase_32 AC 413 ms
4,436 KB
testcase_33 AC 412 ms
4,392 KB
testcase_34 AC 412 ms
4,384 KB
testcase_35 AC 413 ms
4,380 KB
testcase_36 AC 413 ms
4,384 KB
testcase_37 AC 413 ms
4,384 KB
testcase_38 AC 413 ms
4,380 KB
testcase_39 AC 412 ms
4,412 KB
testcase_40 AC 415 ms
4,380 KB
testcase_41 AC 411 ms
4,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main() {
    int T;
    readf("%d\n", T);

    long M = 10 ^^ 9 + 7;

    long div3 = powmod(3U, M.to!ulong-2, M.to!ulong).to!long;

    long[][] mat = [
        [div3, 0, 0, 0, div3, div3],
        [0, div3, 0, div3, 0, div3],
        [0, 0, div3, div3, div3, 0],
        [1L, 0, 0, 0, 0, 0],
        [0, 1L, 0, 0, 0, 0],
        [0, 0, 1L, 0, 0, 0]
    ];

    foreach (i; 0 .. T) {
        long N;
        readf("%d\n", N);

        auto m = matPowMod(mat, N, M);
        m[0][0].writeln;
    }
}

long[][] matMulMod(long[][] a, long[][] b, long m) {
    long l = a[0].length.to!long;

    assert(l == b.length);

    auto res = new long[][](a.length, b[0].length);
    foreach (i, r; res) {
        foreach (k; 0 .. l) {
            foreach (j, ref e; r) {
                e = (e + a[i][k] * b[k][j]) % m;
            }
        }
    }

    return res;
}

long[][] matPowMod(long[][] mat, long p, long m) {
    long l = mat[0].length.to!long;

    assert(l == mat.length);

    auto res = new long[][](l, l);
    foreach (i; 0 .. l) {
        res[i][i] = 1;
    }

    while (p > 0) {
        if (p & 1) {
            res = matMulMod(res, mat, m);
        }
        mat = matMulMod(mat, mat, m);
        p >>= 1;
    }

    return res;
}
0