标签:int sum 就是 fibonacci long 快速 numbers 分解 find
\[
\begin{pmatrix}
f_{n}x^{n} & f_{n+1}x^{n+1} & F_{n}(x)
\end{pmatrix}
\]
\[
=
\begin{pmatrix}
f_{n-1}x^{n-1} & f_{n}x^{n} & F_{n-1}(x)
\end{pmatrix}
\begin{pmatrix}
0 & 0 & x^{2} \ 0 & 1 & 1 \ 1 & 0 & i
\end{pmatrix}
\]
\[
=
\begin{pmatrix}
f_{0}x^{0} & f_{1}x^{1} & F_{1}(x)
\end{pmatrix}
\begin{pmatrix}
0 & 0 & x^{2} \ 0 & 1 & 1 \ 1 & 0 & i
\end{pmatrix}^{n-1}
\]
\[
=
\begin{pmatrix}
0 & x & x
\end{pmatrix}
\begin{pmatrix}
0 & 0 & x^{2} \ 0 & 1 & 1 \ 1 & 0 & i
\end{pmatrix}^{n-1}
\]
#coding: utf-8
from math import sqrt
mod = 1307674368000
def matrix_mult(a, b) :
n = len(a); m = len(b); h = len(b[0])
ans = [[0, 0, 0],[0, 0, 0],[0, 0, 0]]
for i in range(n) :
for j in range(m) :
for k in range(h) :
ans[i][k] += a[i][j] * b[j][k]
if ans[i][k] >= mod :
ans[i][k] %= mod
ans[i][k] %= mod
ans[i][j] %= mod
return ans
def qpower(a, n, i) :
ans = [[0, i, i],[0, 0, 0],[0, 0, 0]]
while n > 0 :
if n & 1 : ans = matrix_mult(ans, a)
n >>= 1
a = matrix_mult(a, a)
return ans[0][2]
if __name__ =="__main__":
ans = 0
for i in range(101):
a = [[0, 0, i ** 2],
[0, 1, 1],
[1, 0, i]]
ans += qpower(a, 10 ** 15 - 1, i)
print( ans % mod )
Project Euler 435 Polynomials of Fibonacci numbers (矩阵快速幂)
标签:int sum 就是 fibonacci long 快速 numbers 分解 find
原文地址:https://www.cnblogs.com/LzyRapx/p/9058311.html