36 lines
938 B
Swift
36 lines
938 B
Swift
let Mod: Int64 = 1000000007
|
|
|
|
func matrix_multiply_2by2(_ A: [[Int64]], _ B: [[Int64]]) -> [[Int64]] {
|
|
var result = Array(repeating: Array(repeating: Int64(0), count: 4), count: 4)
|
|
|
|
result[0][0] = (A[0][0]*B[0][0] + A[0][1]*B[1][0])%Mod
|
|
result[0][1] = (A[0][0]*B[0][1] + A[0][1]*B[1][1])%Mod
|
|
result[1][0] = (A[1][0]*B[0][0] + A[1][1]*B[1][0])%Mod
|
|
result[1][1] = (A[1][0]*B[0][1] + A[1][1]*B[1][1])%Mod
|
|
|
|
return result
|
|
}
|
|
|
|
func power_matrix_2by2(_ A: [[Int64]], exponetioal n: Int64) -> [[Int64]] {
|
|
if n == 0 {
|
|
return [[1,0],[0,1]]
|
|
}
|
|
|
|
if n == 1 {
|
|
return A
|
|
}
|
|
|
|
let half = power_matrix_2by2(A, exponetioal: n/2)
|
|
let result = matrix_multiply_2by2(half, half)
|
|
|
|
if n%2 == 1 {
|
|
return matrix_multiply_2by2(result, A)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
if let n = Int64(readLine() ?? "") {
|
|
print(power_matrix_2by2([[1,1],[1,0]], exponetioal: n)[1][0])
|
|
}
|