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 ...
随机推荐
- 配置CiscoWorks 2000 ANI同步
配置CiscoWorks 2000 ANI同步 在CiscoWorks 2000的LAN ManagementSolution(LMS)中,Cisco包含了一种ANI的自动发现过程和Res ...
- VC眼中的众筹平台:将改变VC募集基金方式,成为下一个纳斯达克市场
“一个好的投资平台可能会成为像纳斯达克一样的市场”,这是投资人给予众筹平台的未来憧憬. ”从长远的角度来说,众筹平台可能会改变VC募集基金的方式“,戈壁投资合伙人蒋涛说,“从二级市场看,不论是企业的I ...
- [Chromium文档转载,第007章]JNI on Chromium for Android
Overview JNI (Java Native Interface) is the mechanism that enables Java code to call native function ...
- Linux常用系统管理软件
1.BleachBit是一款开源的系统清理工具,它可以释放磁盘空间,保护您的隐私,清除缓存,删除cookies.internet历史.临时文件.日志和丢弃的垃圾文件等,支持清除应用的残留数据,切碎文件 ...
- xfce4 + docky ,docky 上面那透明的一条黑色横线去掉方法
在安装完Debian 9 + xfce4桌面后 ,添加docky启动后,会在docky 上面有一条黑色横线看起来非常不舒服. 去掉方法:设置管理器->窗口管理器微调->合成器->取消 ...
- Ubuntu 16.04 实现有线 无线同时用
因为工作的原因,经常会用有线网卡连接服务器进行配置,无线网卡上外网. 一.查看当前网关信息 pipci@ubuntu:~$ ip route showdefault via 192.168.2.1 d ...
- JS中部分 Array 对象方法介绍
1.concat() concat() 方法用于连接两个或多个数组.该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本 <script type="text/javascrip ...
- Swift学习笔记(9)--闭包
1.闭包表达式: { (parameters) -> returnType in statements } 注1.闭包表达式语法可以使用常量.变量和inout类型作为参数,不提供默认值. 也可以 ...
- Spring MVC : Java模板引擎 Thymeleaf (三)
以下以构造一个表单開始,解说 Thymeleaf的使用方法. 为了演示方便,还是以经典的注冊为例. 这是Thymeleaf的form的形式, <form action="#" ...
- struts2学习笔记(7)---数据验证之validateXxx()方法
validateXxx()方法 上一篇文章写了使用Action的validate()方法,validate()仅仅能对action的所有方法进行验证.而要实现对action中特定的方法进行验证,就须要 ...