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 ...
随机推荐
- Web 端 js 导出csv文件
http://www.qdfuns.com/notes/35821/2ab249182734d1f5c66da6b5cf395db9.html
- [Chromium文档转载,第007章]JNI on Chromium for Android
Overview JNI (Java Native Interface) is the mechanism that enables Java code to call native function ...
- 170703 锐姿公司winserver2012 标准版安装过程
背景: 锐姿公司一台服务器,配置为:X3650M5 8871 E5 2620V4 32G 双电源 3*1T raid5 . 原系统由供应商(日闹)上家安装,在安装好的SQL2008,到 ...
- Windows上Python2与Python3同时安装、共存
一.选择 Python2 还是 Python3?当然是全都要 Python3 虽是未来,不过 Python2 的用户群体仍然膨大,网上有大量优良的项目和模块可供使用,遇到问题也基本可以找到解决方法,推 ...
- 【Codeforces Round #424 (Div. 2) A】Unimodal Array
[Link]:http://codeforces.com/contest/831/problem/A [Description] 让你判断一个数列是不是这样一个数列: 一开始是严格上升 然后开始全都是 ...
- Python 爬取单个网页所需要加载的地址和CSS、JS文件地址
Python 爬取单个网页所需要加载的URL地址和CSS.JS文件地址 通过学习Python爬虫,知道根据正式表达式匹配查找到所需要的内容(标题.图片.文章等等).而我从测试的角度去使用Python爬 ...
- 小白学开发(iOS)OC_ SEL数据类型(2015-08-10)
// // main.m // SEL数据类型 // // Created by admin on 15/8/12. // Copyright (c) 2015年 admin. All rig ...
- 指尖上的电商---(10)SolrAdmin中加入多核
在Solr中有的时候,我们并不仅仅是须要一种形式的索引文件.可能须要多种不同数据的索引文件.这时我们就能够在同一个Solr以下创建 多核. 比方,我们在solr以下想把产品信息和分类信息各存放一个索引 ...
- C++ 递归位置排列算法及其应用
废话不多说,我们先看一下位置排序的算法: #include <iostream> using namespace std; int n = 0; int m = 2; int l = 0; ...
- SQL Server字符串分割函数