码迷,mamicode.com
首页 > 其他好文 > 详细

Project Euler 435 Polynomials of Fibonacci numbers (矩阵快速幂)

时间:2018-05-18 23:28:26      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:int   sum   就是   fibonacci   long   快速   numbers   分解   find   

题目链接:

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!)\).

题解:

\[ \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} \]

然后跑矩阵快速幂就可以得到 \(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 )

Project Euler 435 Polynomials of Fibonacci numbers (矩阵快速幂)

标签:int   sum   就是   fibonacci   long   快速   numbers   分解   find   

原文地址:https://www.cnblogs.com/LzyRapx/p/9058311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!