結果

問題 No.1704 Many Bus Stops (easy)
ユーザー lloyzlloyz
提出日時 2021-11-14 22:37:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 794 ms / 2,000 ms
コード長 1,074 bytes
コンパイル時間 400 ms
コンパイル使用メモリ 87,196 KB
実行使用メモリ 80,828 KB
最終ジャッジ日時 2023-08-20 07:35:19
合計ジャッジ時間 24,226 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
76,008 KB
testcase_01 AC 794 ms
79,604 KB
testcase_02 AC 332 ms
78,428 KB
testcase_03 AC 336 ms
78,612 KB
testcase_04 AC 339 ms
78,424 KB
testcase_05 AC 335 ms
79,664 KB
testcase_06 AC 334 ms
78,696 KB
testcase_07 AC 344 ms
80,080 KB
testcase_08 AC 332 ms
78,320 KB
testcase_09 AC 336 ms
78,908 KB
testcase_10 AC 337 ms
78,392 KB
testcase_11 AC 339 ms
80,828 KB
testcase_12 AC 346 ms
78,472 KB
testcase_13 AC 341 ms
79,628 KB
testcase_14 AC 344 ms
78,884 KB
testcase_15 AC 337 ms
79,912 KB
testcase_16 AC 338 ms
79,388 KB
testcase_17 AC 338 ms
78,816 KB
testcase_18 AC 335 ms
78,252 KB
testcase_19 AC 339 ms
79,712 KB
testcase_20 AC 343 ms
79,316 KB
testcase_21 AC 342 ms
78,604 KB
testcase_22 AC 687 ms
78,840 KB
testcase_23 AC 688 ms
78,772 KB
testcase_24 AC 696 ms
79,240 KB
testcase_25 AC 697 ms
78,904 KB
testcase_26 AC 685 ms
78,812 KB
testcase_27 AC 693 ms
78,756 KB
testcase_28 AC 694 ms
78,596 KB
testcase_29 AC 695 ms
79,236 KB
testcase_30 AC 691 ms
79,440 KB
testcase_31 AC 694 ms
78,628 KB
testcase_32 AC 685 ms
78,604 KB
testcase_33 AC 691 ms
78,684 KB
testcase_34 AC 688 ms
79,176 KB
testcase_35 AC 699 ms
79,144 KB
testcase_36 AC 694 ms
80,128 KB
testcase_37 AC 695 ms
78,864 KB
testcase_38 AC 691 ms
78,520 KB
testcase_39 AC 693 ms
79,136 KB
testcase_40 AC 687 ms
78,808 KB
testcase_41 AC 696 ms
78,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 10**9 + 7

def matmul(A, B):
    Ah, Bh, Bw = len(A), len(B), len(B[0])
    C = [[0 for _ in range(Bw)] for _ in range(Ah)]
    for i in range(Ah):
        for j in range(Bw):
            for k in range(Bh):
                C[i][j] += A[i][k] * B[k][j]
                C[i][j] %= mod
    return C

# Mのk乗を効率的に計算する
def doubling(M, k):
    k -= 1
    Mc = M.copy()
    while k > 0:
        if k & 1 == 1:
            Mc = matmul(Mc, M)
        M = matmul(M, M) # Mの(2のi乗)の乗 を計算する
        k >>= 1
    return Mc

A = [[1, 0, 0, 0, 1, 1],
     [0, 1, 0, 1, 0, 1],
     [0, 0, 1, 1, 1, 0],
     [3, 0, 0, 0, 0, 0],
     [0, 3, 0, 0, 0, 0],
     [0, 0, 3, 0, 0, 0]]
F = [1, 0, 0, 3, 0, 0]
fraction = pow(3, mod - 2, mod)

t = int(input())
for _ in range(t):
    n = int(input())
    if n == 1:
        print(fraction)
    else:
        M = doubling(A, n)
        ans = 0
        for i in range(6):
            ans += M[3][i] * F[i]
            ans %= mod
        ans *= pow(fraction, n + 1, mod)
        ans %= mod
        print(ans)
0