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 ...
随机推荐
- 转:Java读写文件各种方法及性能比较
干Java这么久,一直在做WEB相关的项目,一些基础类差不多都已经忘记.经常想得捡起,但总是因为一些原因,不能如愿. 其实不是没有时间,只是有些时候疲于总结,今得空,下定决心将丢掉的都给捡起来. 文件 ...
- Right turn(四川省第七届)
Right turn Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class n ...
- MYSQL去重复并计算总数的sql语句
SELECT count(distinct uid) FROM `two_users`
- hdparm
https://www.douban.com/note/244813504/ http://blog.sina.com.cn/s/blog_413d250e0101jtr7.html http://m ...
- CODEVS——T 4189 字典
http://codevs.cn/problem/4189/ 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Des ...
- 4.2.2 MINUS
4.2.2 MINUS正在更新内容,请稍后
- HDU - 2254 奥运 (求等比数列和)
Description 北京迎来了第一个奥运会,我们的欢呼声响彻中国大地,所以今年的奥运金牌 day day up! 比尔盖兹坐上鸟巢里,手里摇着小纸扇,看的不亦乐乎,被俺们健儿的顽强拼搏的精神深深的 ...
- nagios插件之登陆防火墙实现session监控
ssh_firewall_session.sh -- 登陆防火墙并运行dis session statistics firewall_check_sessions.c -- 调用上面脚本.过滤出ses ...
- 【数字图像处理】五.MFC图像点运算之灰度线性变化、灰度非线性变化、阈值化和均衡化处理具体解释
本文主要讲述基于VC++6.0 MFC图像处理的应用知识,主要结合自己大三所学课程<数字图像处理>及课件进行解说.主要通过MFC单文档视图实现显示BMP图片点运算处理.包含图像灰度线性变换 ...
- Fedora Core 11 Alpha试用视频(由于youtube问题暂时无法访问)
1.系统安装: http://www.youtube.com/watch?v=QcDy5eRQZ20 2.重启后配置 3.体验Fedora 11 Gnome2.25和Kde 4.2 http: ...