結果

問題 No.1706 Many Bus Stops (hard)
ユーザー LyricalMaestroLyricalMaestro
提出日時 2024-02-09 01:38:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 1,764 bytes
コンパイル時間 257 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 61,856 KB
最終ジャッジ日時 2024-02-09 01:38:52
合計ジャッジ時間 3,722 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
53,460 KB
testcase_01 AC 32 ms
53,460 KB
testcase_02 AC 38 ms
61,856 KB
testcase_03 AC 39 ms
61,856 KB
testcase_04 AC 46 ms
61,856 KB
testcase_05 AC 50 ms
61,856 KB
testcase_06 AC 38 ms
59,712 KB
testcase_07 AC 37 ms
61,856 KB
testcase_08 AC 38 ms
61,856 KB
testcase_09 AC 38 ms
61,856 KB
testcase_10 AC 39 ms
61,856 KB
testcase_11 AC 46 ms
61,856 KB
testcase_12 AC 39 ms
61,856 KB
testcase_13 AC 38 ms
61,856 KB
testcase_14 AC 40 ms
61,856 KB
testcase_15 AC 45 ms
61,856 KB
testcase_16 AC 39 ms
61,856 KB
testcase_17 AC 40 ms
61,856 KB
testcase_18 AC 40 ms
61,856 KB
testcase_19 AC 39 ms
61,856 KB
testcase_20 AC 41 ms
61,856 KB
testcase_21 AC 37 ms
61,856 KB
testcase_22 AC 37 ms
59,712 KB
testcase_23 AC 38 ms
61,856 KB
testcase_24 AC 38 ms
61,856 KB
testcase_25 AC 38 ms
61,856 KB
testcase_26 AC 62 ms
61,856 KB
testcase_27 AC 38 ms
59,712 KB
testcase_28 AC 38 ms
61,856 KB
testcase_29 AC 38 ms
61,856 KB
testcase_30 AC 40 ms
59,712 KB
testcase_31 AC 39 ms
61,856 KB
testcase_32 AC 41 ms
61,856 KB
testcase_33 AC 40 ms
61,856 KB
testcase_34 AC 39 ms
61,856 KB
testcase_35 AC 39 ms
59,712 KB
testcase_36 AC 39 ms
61,856 KB
testcase_37 AC 39 ms
59,712 KB
testcase_38 AC 38 ms
61,856 KB
testcase_39 AC 38 ms
61,856 KB
testcase_40 AC 38 ms
59,712 KB
testcase_41 AC 37 ms
61,856 KB
testcase_42 AC 37 ms
61,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/1706

MOD = 10 ** 9 + 7

def prod(matrix1, matrix2):
    new_matrix = [[0] * 4 for _ in range(4)]
    for i in range(4):
        for j in range(4):
            for k in range(4):
                new_matrix[i][j] += (matrix1[i][k] * matrix2[k][j]) % MOD
                new_matrix[i][j] %= MOD
    return new_matrix

def main():
    C, N, M = map(int, input().split())

    # バス1台について「N時点でバス停1にいない」確率を求める
    # 0: バス停1にいる組み合わせ
    # 1: バス停x(x>=2)にいる組み合わせ
    # 2: バス停1に向かっている最中の組み合わせ
    # 3: バス停x(x>=2)に向かっている最中の組み合わせ
    dp = [0] * 4
    dp[0] = 1
    inv_c = pow(C, MOD - 2, MOD)
    inv_c1 = ((C - 1) * inv_c) % MOD
    inv_c2 = ((C - 2) * inv_c) % MOD


    matrix = [[0] * 4 for _ in range(4)]
    matrix[0][0] = inv_c
    matrix[3][0] = inv_c1
    matrix[1][1] = inv_c
    matrix[2][1] = inv_c
    matrix[3][1] = inv_c2
    matrix[0][2] = 1
    matrix[1][3] = 1

    base_matrix = [[0] * 4 for _ in range(4)]
    for i in range(4):
        base_matrix[i][i] = 1    
    while N > 0:
        if N % 2 == 1:
            base_matrix = prod(base_matrix, matrix)
        
        matrix = prod(matrix, matrix)
        N //= 2

    init_vector = [1, 0, 0, 0]
    vector = [0] * 4
    for i in range(4):
        for j in range(4):
            vector[i] += (base_matrix[i][j] * init_vector[j]) % MOD
            vector[i] %= MOD

    ans = 0
    for i in range(1, 4):
        ans += vector[i]
        ans %= MOD

    # Mのバスの分だけかける
    ans = pow(ans, M, MOD)
    answer = (1 - ans) % MOD
    print(answer)


if __name__ == "__main__":
    main()
0