結果
問題 | No.1073 無限すごろく |
ユーザー |
![]() |
提出日時 | 2025-03-26 15:50:32 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 48 ms / 2,000 ms |
コード長 | 1,934 bytes |
コンパイル時間 | 357 ms |
コンパイル使用メモリ | 82,696 KB |
実行使用メモリ | 62,376 KB |
最終ジャッジ日時 | 2025-03-26 15:51:30 |
合計ジャッジ時間 | 2,975 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
MOD = 10**9 + 7def main():import sysn = int(sys.stdin.readline())inv6 = pow(6, MOD-2, MOD)# Precompute p[0] to p[5]p = [0] * 6p[0] = 1if n == 0:print(p[0])returnfor i in range(1, 6):s = 0for j in range(1, i + 1):s = (s + p[i - j]) % MODp[i] = s * inv6 % MODif n < 6:print(p[n])return# Define the transition matrixmat = [[inv6] * 6,[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]]# Initial vector is [p5, p4, p3, p2, p1, p0]initial_vector = [p[5], p[4], p[3], p[2], p[1], p[0]]power = n - 5# Function to multiply two matricesdef multiply(a, b):res = [[0]*6 for _ in range(6)]for i in range(6):for k in range(6):if a[i][k] == 0:continuefor j in range(6):res[i][j] = (res[i][j] + a[i][k] * b[k][j]) % MODreturn res# Function to multiply matrix and vectordef multiply_mat_vec(mat, vec):res = [0] * 6for i in range(6):for j in range(6):res[i] = (res[i] + mat[i][j] * vec[j]) % MODreturn res# Matrix exponentiationresult_mat = [[0]*6 for _ in range(6)]for i in range(6):result_mat[i][i] = 1 # Identity matrixcurrent_mat = [row[:] for row in mat]while power > 0:if power % 2 == 1:result_mat = multiply(result_mat, current_mat)current_mat = multiply(current_mat, current_mat)power //= 2# Multiply the resulting matrix with the initial vectorfinal_vec = multiply_mat_vec(result_mat, initial_vector)print(final_vec[0])if __name__ == "__main__":main()