結果

問題 No.1073 無限すごろく
ユーザー wattaiheiwattaihei
提出日時 2020-06-05 21:57:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 1,254 bytes
コンパイル時間 187 ms
コンパイル使用メモリ 10,928 KB
実行使用メモリ 8,380 KB
最終ジャッジ日時 2023-08-22 15:19:34
合計ジャッジ時間 2,011 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,324 KB
testcase_01 AC 18 ms
8,312 KB
testcase_02 AC 18 ms
8,172 KB
testcase_03 AC 17 ms
8,056 KB
testcase_04 AC 17 ms
8,264 KB
testcase_05 AC 16 ms
8,228 KB
testcase_06 AC 16 ms
8,232 KB
testcase_07 AC 17 ms
8,324 KB
testcase_08 AC 16 ms
8,276 KB
testcase_09 AC 17 ms
8,268 KB
testcase_10 AC 17 ms
8,276 KB
testcase_11 AC 16 ms
8,276 KB
testcase_12 AC 16 ms
8,380 KB
testcase_13 AC 18 ms
8,288 KB
testcase_14 AC 18 ms
8,272 KB
testcase_15 AC 18 ms
8,320 KB
testcase_16 AC 18 ms
8,344 KB
testcase_17 AC 17 ms
8,276 KB
testcase_18 AC 18 ms
8,272 KB
testcase_19 AC 17 ms
8,308 KB
testcase_20 AC 18 ms
8,356 KB
testcase_21 AC 18 ms
8,264 KB
testcase_22 AC 18 ms
8,268 KB
testcase_23 AC 19 ms
8,264 KB
testcase_24 AC 20 ms
8,268 KB
testcase_25 AC 19 ms
8,276 KB
testcase_26 AC 20 ms
8,324 KB
testcase_27 AC 20 ms
8,276 KB
testcase_28 AC 21 ms
8,268 KB
testcase_29 AC 22 ms
8,336 KB
testcase_30 AC 21 ms
8,348 KB
testcase_31 AC 22 ms
8,348 KB
testcase_32 AC 22 ms
8,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 10**9+7

# matrix power
def matintersection(matA, matB, mod=mod):
    length = len(matA)
    matR = [[0]*length for _ in range(length)]
    for i in range(length):
        for j in range(length):
            for k in range(length):
                matR[i][j] += matA[i][k]*matB[k][j] % mod
                matR[i][j] %= mod
    return matR

def power_mat(matA, n, mod=mod):
    bi = str(format(n,"b"))#2進表現に
    length = len(matA)
    res = [[1 if i == j else 0 for i in range(length)] for j in range(length)]
    for i in range(len(bi)):
        res = matintersection(res, res, mod)
        if bi[i] == "1":
            res = matintersection(res, matA, mod)
    return res

N = int(input())

inv6 = pow(6, mod-2, mod)
vec = [1]
for _ in range(5):
    c = 0
    for v in vec:
        c += v
    vec.append(c*inv6%mod)

if N <= 5:
    ans = vec[N]
else:
    vec = vec[::-1]
    mat = [[inv6, inv6, inv6, inv6, inv6, inv6],
            [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]
    ]
    ret_mat = power_mat(mat, N-5, mod)
    tmp = 0
    for i in range(6):
        tmp = (tmp + ret_mat[0][i]*vec[i]) % mod
    ans = tmp % mod
print(ans)
0