結果

問題 No.1073 無限すごろく
ユーザー wattaiheiwattaihei
提出日時 2020-06-05 21:57:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 1,254 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-05-09 21:02:23
合計ジャッジ時間 2,189 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
10,496 KB
testcase_01 AC 25 ms
10,752 KB
testcase_02 AC 26 ms
10,496 KB
testcase_03 AC 24 ms
10,752 KB
testcase_04 AC 24 ms
10,624 KB
testcase_05 AC 24 ms
10,752 KB
testcase_06 AC 24 ms
10,880 KB
testcase_07 AC 24 ms
10,624 KB
testcase_08 AC 25 ms
10,752 KB
testcase_09 AC 25 ms
10,624 KB
testcase_10 AC 24 ms
10,624 KB
testcase_11 AC 24 ms
10,752 KB
testcase_12 AC 25 ms
10,880 KB
testcase_13 AC 25 ms
10,880 KB
testcase_14 AC 26 ms
10,752 KB
testcase_15 AC 26 ms
10,496 KB
testcase_16 AC 26 ms
10,752 KB
testcase_17 AC 26 ms
10,496 KB
testcase_18 AC 26 ms
10,624 KB
testcase_19 AC 26 ms
10,624 KB
testcase_20 AC 26 ms
10,880 KB
testcase_21 AC 25 ms
10,880 KB
testcase_22 AC 25 ms
10,752 KB
testcase_23 AC 27 ms
10,880 KB
testcase_24 AC 27 ms
10,880 KB
testcase_25 AC 27 ms
10,752 KB
testcase_26 AC 28 ms
10,752 KB
testcase_27 AC 38 ms
10,752 KB
testcase_28 AC 39 ms
10,752 KB
testcase_29 AC 41 ms
10,624 KB
testcase_30 AC 40 ms
10,624 KB
testcase_31 AC 41 ms
10,880 KB
testcase_32 AC 40 ms
10,880 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