結果

問題 No.1704 Many Bus Stops (easy)
ユーザー nebukuro09nebukuro09
提出日時 2021-10-08 22:54:27
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 1,489 bytes
コンパイル時間 602 ms
コンパイル使用メモリ 99,768 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-04 14:24:33
合計ジャッジ時間 10,151 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 262 ms
4,380 KB
testcase_02 AC 89 ms
4,376 KB
testcase_03 AC 90 ms
4,376 KB
testcase_04 AC 90 ms
4,380 KB
testcase_05 AC 90 ms
4,380 KB
testcase_06 AC 90 ms
4,384 KB
testcase_07 AC 90 ms
4,384 KB
testcase_08 AC 89 ms
4,384 KB
testcase_09 AC 90 ms
4,380 KB
testcase_10 AC 89 ms
4,380 KB
testcase_11 AC 90 ms
4,380 KB
testcase_12 AC 90 ms
4,376 KB
testcase_13 AC 89 ms
4,384 KB
testcase_14 AC 89 ms
4,384 KB
testcase_15 AC 89 ms
4,376 KB
testcase_16 AC 89 ms
4,380 KB
testcase_17 AC 90 ms
4,376 KB
testcase_18 AC 89 ms
4,380 KB
testcase_19 AC 90 ms
4,380 KB
testcase_20 AC 90 ms
4,384 KB
testcase_21 AC 90 ms
4,380 KB
testcase_22 AC 268 ms
4,376 KB
testcase_23 AC 268 ms
4,380 KB
testcase_24 AC 267 ms
4,380 KB
testcase_25 AC 269 ms
4,380 KB
testcase_26 AC 265 ms
4,376 KB
testcase_27 AC 267 ms
4,380 KB
testcase_28 AC 267 ms
4,376 KB
testcase_29 AC 271 ms
4,380 KB
testcase_30 AC 269 ms
4,380 KB
testcase_31 AC 269 ms
4,380 KB
testcase_32 AC 267 ms
4,376 KB
testcase_33 AC 267 ms
4,380 KB
testcase_34 AC 267 ms
4,376 KB
testcase_35 AC 266 ms
4,376 KB
testcase_36 AC 265 ms
4,380 KB
testcase_37 AC 270 ms
4,380 KB
testcase_38 AC 266 ms
4,380 KB
testcase_39 AC 269 ms
4,380 KB
testcase_40 AC 265 ms
4,384 KB
testcase_41 AC 267 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;

immutable long MOD = 10^^9 + 7;

void main() {
    auto T = readln.chomp.to!int;

    long a = powmod(3, MOD-2, MOD);
    long b = 1L;

    long[][] X = [[a, 0, 0, a, 0, a],
                  [0, a, 0, a, a, 0],
                  [0, 0, a, 0, a, a],
                  [0, 0, b, 0, 0, 0],
                  [b, 0, 0, 0, 0, 0],
                  [0, b, 0, 0, 0, 0]];

    while (T--) {
        auto N = readln.chomp.to!int;
        auto ans = matpow(6, N, X);
        ans[0][0].writeln;
    }
}

long powmod(long a, long x, long m) {
    long ret = 1;
    while (x) {
        if (x % 2) ret = ret * a % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}


long[][] matmul(int N, long[][] m1, long[][] m2) {
    auto ret = new long[][](N, N);
    foreach (i; 0..N) {
        foreach (j; 0..N) {
            ret[i][j] = 0;
            foreach (k; 0..N) {
                (ret[i][j] += m1[i][k] * m2[k][j] % MOD) %= MOD;
            }
        }
    }
    return ret;
}

long[][] matpow(int N, long K, long[][] mat) {
    auto ret = new long[][](N, N);
    foreach (i; 0..N)
        foreach (j; 0..N)
            ret[i][j] = i == j ? 1 : 0;

    while (K > 0) {
        if (K % 2 == 1)
            ret = matmul(N, ret, mat);
        mat = matmul(N, mat, mat);
        K /= 2;
    }

    return ret;
}
0