結果

問題 No.1073 無限すごろく
ユーザー yuppe19 😺yuppe19 😺
提出日時 2020-06-13 21:29:35
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 1,273 bytes
コンパイル時間 130 ms
コンパイル使用メモリ 10,784 KB
実行使用メモリ 8,180 KB
最終ジャッジ日時 2023-09-08 04:46:59
合計ジャッジ時間 3,579 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,180 KB
testcase_01 AC 16 ms
7,972 KB
testcase_02 AC 17 ms
7,972 KB
testcase_03 AC 16 ms
8,048 KB
testcase_04 AC 16 ms
8,124 KB
testcase_05 AC 16 ms
8,072 KB
testcase_06 AC 16 ms
8,068 KB
testcase_07 AC 16 ms
8,052 KB
testcase_08 AC 16 ms
8,164 KB
testcase_09 AC 16 ms
8,060 KB
testcase_10 AC 16 ms
8,052 KB
testcase_11 AC 16 ms
8,056 KB
testcase_12 AC 16 ms
8,064 KB
testcase_13 AC 18 ms
8,052 KB
testcase_14 AC 17 ms
8,060 KB
testcase_15 AC 17 ms
8,124 KB
testcase_16 AC 19 ms
8,036 KB
testcase_17 AC 17 ms
8,064 KB
testcase_18 AC 17 ms
8,064 KB
testcase_19 AC 18 ms
8,040 KB
testcase_20 AC 17 ms
8,056 KB
testcase_21 AC 18 ms
8,044 KB
testcase_22 AC 18 ms
8,180 KB
testcase_23 AC 19 ms
8,056 KB
testcase_24 AC 20 ms
8,036 KB
testcase_25 AC 19 ms
8,124 KB
testcase_26 AC 20 ms
8,056 KB
testcase_27 AC 19 ms
8,128 KB
testcase_28 AC 22 ms
7,992 KB
testcase_29 AC 22 ms
7,988 KB
testcase_30 AC 22 ms
8,152 KB
testcase_31 AC 21 ms
8,036 KB
testcase_32 AC 22 ms
7,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/python3
# †
mod = 10**9 + 7

def mod_inv(a, m):
    b, s, t, old_a = m, 1, 0, a
    while b:
        q = a // b
        a %= b
        a, b = b, a
        s -= t * q
        s, t = t, s
    if a != 1:
        print('Error: {}**(-1) mod {} does not exist.'.format(old_a, m))
        return None
    return s + m if s < 0 else s


def matmul(A, B):
    p, q = len(A[0]), len(B)
    if p != q:
        raise ValueError('len(A[0])={} len(B)={}'.format(p, q))
    rows, cols, times = len(A), len(B[0]), len(A[0])
    res = [[0] * cols for _ in range(rows)]
    for r in range(rows):
        for c in range(cols):
            res[r][c] = sum(A[r][i]*B[i][c] % mod for i in range(times))
            res[r][c] %= mod
    return res


def matpow(A, n):
    m = len(A)
    res = [[int(i==j) for j in range(m)] for i in range(m)]
    while n > 0:
        if n & 1:
            res = matmul(res, A)
        A = matmul(A, A)
        n >>= 1
    return res


###
if __name__ == '__main__':
    n = int(input())
    x = mod_inv(6, mod)
    A = [
        [x, x, x, x, x, x],
        [1, 0, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 0, 1, 0, 0, 0],
        [0, 0, 0, 1, 0, 0],
        [0, 0, 0, 0, 1, 0],
    ]
    R = matpow(A, n)
    res = R[0][0]
    print(res)
0