fibonacci数列(二)_矩阵快速幂
描述
In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
An alternative formula for the Fibonacci sequence is
.
Given an integer n, your goal is to compute the last 4 digits of Fn.
Hint
As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by
.
Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:
.
- 输入
- The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.
- 输出
- For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).
- 样例输入
-
0
9
1000000000
-1 - 样例输出
-
0
34
6875 【题意】斐波那契数列可以用矩阵来求
当求第非常大的一个斐波那契数的后几位时我们可以用矩阵快速幂求解了。
#include<iostream>
#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
typedef vector<int>vec;
typedef vector<vec>mat;
const int N=;
mat mul(mat a,mat b)//求两个矩阵的乘积
{
mat c(a.size(),vec(b[].size()));
for(int i=;i<a.size();i++)
{
for(int k=;k<b.size();k++)
{
for(int j=;j<b[].size();j++)
{
c[i][j]=(c[i][j]+a[i][k]*b[k][j])%N;
}
}
}
return c;
}
mat get_ans(mat a,int n)//矩阵的快速幂
{
mat b(a.size(),vec(a.size()));
for(int i=;i<a.size();i++)
{
b[i][i]=;
}
while(n>)
{
if(n&) b=mul(b,a);
a=mul(a,a);
n>>=;
}
return b;
}
int main()
{
long long int n;
while(~scanf("%lld",&n),n>=)
{
if(n==-) break;
mat a(,vec());
a[][]=,a[][]=;
a[][]=,a[][]=;
a=get_ans(a,n);
printf("%d\n",a[][]);
}
return ;
}
fibonacci数列(二)_矩阵快速幂的更多相关文章
- nyoj_148_fibonacci数列(二)_矩阵快速幂
fibonacci数列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 In the Fibonacci integer sequence, F0 = 0, F ...
- HDU——1005Number Sequence(模版题 二维矩阵快速幂+操作符重载)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- leetcode_935. Knight Dialer_动态规划_矩阵快速幂
https://leetcode.com/problems/knight-dialer/ 在如下图的拨号键盘上,初始在键盘中任意位置,按照国际象棋中骑士(中国象棋中马)的走法走N-1步,能拨出多少种不 ...
- POJ3070 斐波那契数列递推 矩阵快速幂模板题
题目分析: 对于给出的n,求出斐波那契数列第n项的最后4为数,当n很大的时候,普通的递推会超时,这里介绍用矩阵快速幂解决当递推次数很大时的结果,这里矩阵已经给出,直接计算即可 #include< ...
- bzoj5118 Fib数列2 二次剩余+矩阵快速幂
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5118 题解 这个题一看就是不可做的样子. 求斐波那契数列的第 \(n\) 项,\(n \leq ...
- Tribonacci UVA - 12470 (简单的斐波拉契数列)(矩阵快速幂)
题意:a1=0;a2=1;a3=2; a(n)=a(n-1)+a(n-2)+a(n-3); 求a(n) 思路:矩阵快速幂 #include<cstdio> #include<cst ...
- hihoCoder #1151 : 骨牌覆盖问题·二 (矩阵快速幂,DP)
题意:给一个3*n的矩阵,要求用1*2的骨牌来填满,有多少种方案? 思路: 官网题解用的仍然是矩阵快速幂的方式.复杂度O(logn*83). 这样做需要构造一个23*23的矩阵,这个矩阵自乘n-1次, ...
- BZOJ5118 Fib数列2(矩阵快速幂)
特殊矩阵的幂同样满足费马小定理. #include<iostream> #include<cstdio> #include<cmath> #include<c ...
- BZOJ 3231: [Sdoi2008]递归数列 (JZYZOJ 1353) 矩阵快速幂
http://www.lydsy.com/JudgeOnline/problem.php?id=3231 和斐波那契一个道理在最后加一个求和即可 #include<cstdio> #i ...
随机推荐
- hdu----(1950)Bridging signals(最长递增子序列 (LIS) )
Bridging signals Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 去除DataTable重复数据的三种方法
业务需求 最近做一个把源数据库的数据批次导出到目标数据库.源数据库是采集程序采集而来的原始数据库,所以需要对其进行一些处理(过滤一些为空,长度太短或太长,非法字符,重复数据)然后在进行入库. 其中要避 ...
- 意外的节点类型 Element。只能在简单内容或空内容上调用 ReadElementString 方法
问题出现的情景: 在调用携程团购接口时,需要把获取的xml字符串反序列化实体对象,出现了这个错误. 详情: 在对xml文档有这样一条语句“ <Description Category=" ...
- Response返回JSON数据到前台页面
转自博文:<Response JSON数据返回>http://blog.csdn.net/anialy/article/details/8665471 简述: 在servlet填充Resp ...
- android 定制目录
首先简单介绍一下安卓系统文件夹对照表 主要介绍的是Android系统的文件夹结构,帮助大家更直观地了解系统 \\system\\app这个里面主要存放的是常规下载的应用程序,可以看到都是以APK格式结 ...
- eclipse快捷键失效的解决办法
今天敲html代码,突然发现ctrl+D不能用了,shift+enter/shift+ctrl+enter也不能用了,上网上搜了下,原来我是在文本模式下打开的.切换为html editor打开就o了. ...
- Operating System Concepts with java 项目: Shell Unix 和历史特点
线程间通信,fork(),waitpid(),signal,捕捉信号,用c执行shell命令,共享内存,mmap 实验要求: 1.简单shell: 通过c实现基本的命令行shell操作,实现两个函数, ...
- HDU 2676 Network Wars 01分数规划,最小割 难度:4
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1676 对顶点i,j,起点s=1,终点t=n,可以认为题意要求一组01矩阵use ...
- WP8 学习 ApplicationBar 的创建 XAML代码
phone:PhoneApplicationPage.ApplicationBar> <shell:ApplicationBar Opacity="0.1" IsVis ...
- String字符串包含运算符实现运算
string aa = "(1+2)/3+(3+4)*5"; DataTable dt = new DataTable(); string b = dt.Compute(aa, & ...