题目链接:
https://projecteuler.net/problem=435
题意:
The Fibonacci numbers $ {f_n, n ≥ 0}$ are defined recursively as (f_n = f_{n-1} + f_{n-2}) with base cases (f_0 = 0) and (f_1 = 1).
Define the polynomials $ {F_n, n ≥ 0} $ as $F_n(x) =sum_{i=0}^{n} f_i x^i $.
For example, (F_{7}(x) = x + x^2 + 2x^3 + 3x^4 + 5x^5 + 8x^6 + 13x^7), and$ F_7(11) = 268357683$.
Let (n = 10^{15}). Find [$sum_{x=0}^{100} F_{n}(x)] $ mod (1307674368000 (= 15!)).
题解:
[egin{pmatrix}
f_{n}x^{n} & f_{n+1}x^{n+1} & F_{n}(x)
end{pmatrix}
]
[=
egin{pmatrix}
f_{n-1}x^{n-1} & f_{n}x^{n} & F_{n-1}(x)
end{pmatrix}
egin{pmatrix}
0 & 0 & x^{2} \
0 & 1 & 1 \
1 & 0 & i
end{pmatrix}
]
[=
egin{pmatrix}
f_{0}x^{0} & f_{1}x^{1} & F_{1}(x)
end{pmatrix}
egin{pmatrix}
0 & 0 & x^{2} \
0 & 1 & 1 \
1 & 0 & i
end{pmatrix}^{n-1}
]
[=
egin{pmatrix}
0 & x & x
end{pmatrix}
egin{pmatrix}
0 & 0 & x^{2} \
0 & 1 & 1 \
1 & 0 & i
end{pmatrix}^{n-1}
]
然后跑矩阵快速幂就可以得到 (F_{n}(x))了。(C)++ 会爆 (long long)... 用 (Python)吧...
其实用 (C)++也行,就是将模数分解再用 (crt) 合并。
代码:
#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 )