Project Euler 435 Polynomials of Fibonacci numbers (矩阵快速幂)
题目链接:
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!)\).
题解:
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 (矩阵快速幂)的更多相关文章
- hdu 3117 Fibonacci Numbers 矩阵快速幂+公式
斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...
- hdu3306 Another kind of Fibonacci【矩阵快速幂】
转载请注明出处:http://www.cnblogs.com/KirisameMarisa/p/4187670.html 题目链接:http://acm.hdu.edu.cn/showproblem. ...
- POJ 3070 Fibonacci 【矩阵快速幂】
<题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...
- Count Numbers(矩阵快速幂)
Count Numbers 时间限制: 8 Sec 内存限制: 128 MB提交: 43 解决: 19[提交] [状态] [讨论版] [命题人:admin] 题目描述 Now Alice want ...
- poj 3070 Fibonacci(矩阵快速幂,简单)
题目 还是一道基础的矩阵快速幂. 具体的居者的幂公式我就不明示了. #include<stdio.h> #include<string.h> #include<algor ...
- POJ 3070 Fibonacci(矩阵快速幂)
题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. #include <iostream> #include <cstring> #includ ...
- 2018.09.25 poj3070 Fibonacci(矩阵快速幂)
传送门 矩阵快速幂板题,写一道来练练手. 这一次在poj做题总算没忘了改万能库. 代码: #include<iostream> #include<cstdio> #define ...
- poj3070 Fibonacci(矩阵快速幂)
矩阵快速幂基本应用. 对于矩阵乘法与递推式之间的关系: 如:在斐波那契数列之中 f[i] = 1*f[i-1]+1*f[i-2] f[i-1] = 1*f[i-1] + 0*f[i-2].即 所以, ...
- HDU:Gauss Fibonacci(矩阵快速幂+二分)
http://acm.hdu.edu.cn/showproblem.php?pid=1588 Problem Description Without expecting, Angel replied ...
随机推荐
- Linux企业应用--RHAS 2.1 下安装中文 Lotus Domino R 6.5 图解
原文请到ftp.jms165.com下载,是用上传用户 (RHAS3+ksnapshot+OperOff ...
- 紫书 例题 9-9 UVa 10003 (区间dp+递推顺序)
区间dp,可以以一个区间为状态,f[i][j]是第i个切点到第j个切点的木棍的最小费用 那么对于当前这一个区间,枚举切点k, 可以得出f[i][j] = min{dp(i, k) + dp(k, j) ...
- MD5解密(常用语登录密码加密)
http://pmd5.com/
- 【Henu ACM Round #13 E】Spy Syndrome 2
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 对m个串建立一棵字典树. 然后对主串. 尝试从第一个位置开始.在字典树中尝试匹配 如果匹配到了位置i 就再从位置i+1开始尝试匹配 ...
- 大型情感类电视连续剧--Android高德之旅(2)地图类型
总要说两句 今天继续我们的Android高德之旅,上一篇已经能够显示最主要的地图了.有主要的放大缩小功能.还有最后做的点击3D旋转.倾斜视角的效果.今天这篇文章来记录一下高德地图的5种地图类型. (其 ...
- js---08函数 定时器
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 6.cocos2d设置定时器
T1LayerAnchorPoint.h #pragma once #include "cocos2d.h" USING_NS_CC; class T1LayerAnchorPoi ...
- Linux集群的I/O性能测试
Linux集群的I/O性能测试 本文介绍利用iozone的性能测试工具,来测试集群性能.测试步骤如下:1.在Server节点上安装iozone(可以到www.iozone.org上下载) #rpm ...
- linux操作指令:
系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...
- python django的单元测试
# TestCase 基类的使用 django.test.TestCase # 测试依赖于数据库访问,创建或查询模型 unittest.TestCase #没有与数据库交互 避免刷新数据库的成本