Fibonacci Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1362    Accepted Submission(s): 564

Problem Description
The Fibonacci sequence is the sequence of numbers such that every element is equal to the sum of the two previous elements, except for the first two elements f0 and f1 which are respectively zero and one.

What is the numerical value of the nth Fibonacci number?

 
Input
For each test case, a line will contain an integer i between 0 and 10
8 inclusively, for which you must compute the ith Fibonacci number fi. Fibonacci numbers get large pretty quickly, so whenever the answer has more than 8 digits, output only the first and last 4 digits of the answer, separating the two parts with an ellipsis (“...”).

There is no special way to denote the end of the of the input, simply stop when the standard input terminates (after the EOF).

 
Sample Input
0
1
2
3
4
5
35
36
37
38
39
40
64
65
 
Sample Output
0
1
1
2
3
5
9227465
14930352
24157817
39088169
63245986
1023...4155
1061...7723
1716...7565

分析:求F[n]的后四位可以用矩阵快速幂求

重点在于如何求F[n]的前四位:

已知F[n]的通项公式:=F[n]=d.xxx * 10^m;//d<10

则Log10(F[n])=m+log10(d.xxx)=log10(an),容易知道F[n]的前四位和m无关,只和d.xxx有关,所以现在就是如何求d.xxx了,an是已知的且n>=40时-(1-sqrt(5))^n/2^n太小了,不影响前四位,所以可以舍去,则只要求log10(an)中的log10(1/sqrt(5))+n*log((1+sqrt(5))/2)得到m.xxx只要m.xxx的小数部分0.xxx即可,0.xxx=log10(d.xxx),然后d.xxx=pow(10.0,0.xxx)

注:由计算可知n<40时F[n]<100000000,可以直接输出

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<math.h>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=2;
const int mod=10000;
int array[MAX][MAX],sum[MAX][MAX];
int F[40]; void MatrixMult(int a[2][2],int b[2][2]){
int c[2][2]={0};
for(int i=0;i<2;++i){
for(int j=0;j<2;++j){
for(int k=0;k<2;++k){
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(int i=0;i<2;++i){
for(int j=0;j<2;++j)a[i][j]=c[i][j]%mod;
}
} int Matrix(int k){
array[0][0]=array[0][1]=array[1][0]=1;
array[1][1]=0;
sum[0][0]=sum[1][1]=1;
sum[0][1]=sum[1][0]=0;
while(k){
if(k&1)MatrixMult(sum,array);
MatrixMult(array,array);
k>>=1;
}
return sum[0][0];
} int pre(int n){
double a=sqrt(5.0);
double b=(1+a)/2;
a=1/a;
double s=log10(a)+n*log10(b);
s=s-(int)s;
double d=pow(10.0,s);
return int(d*1000);
} void Init(){//经计算发现n>=40时F[n]>=100000000
F[0]=0,F[1]=1;
for(int i=2;i<40;++i)F[i]=F[i-1]+F[i-2];
} int main(){
Init();
int n;
while(cin>>n){
if(n<40)cout<<F[n]<<endl;
else{
cout<<pre(n);//输出前4位,前4位用F[n]的通项公式求
cout<<"...";
cout<<setfill('0')<<setw(4)<<Matrix(n-1)<<endl;//输出后4位,后四位用矩阵快速幂求
}
}
return 0;
}

hdu3117之矩阵快速幂的更多相关文章

  1. HDU 3117 Fibonacci Numbers( 矩阵快速幂 + 数学推导 )

    链接:传送门 题意:给一个 n ,输出 Fibonacci 数列第 n 项,如果第 n 项的位数 >= 8 位则按照 前4位 + ... + 后4位的格式输出 思路: n < 40时位数不 ...

  2. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  3. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  4. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  5. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  6. HDU5950(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...

  7. 51nod 1126 矩阵快速幂 水

    有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...

  8. hdu2604(递推,矩阵快速幂)

    题目链接:hdu2604 这题重要的递推公式,找到公式就很easy了(这道题和hdu1757(题解)类似,只是这道题需要自己推公式) 可以直接找规律,推出递推公式,也有另一种找递推公式的方法:(PS: ...

  9. 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式

    矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d     ...

随机推荐

  1. thinkphp3.2局部不缓存的静态缓存

    在thinkphp中,对于访问量大的网站可以用静态缓存来越过数据库瓶颈来提高访问速度,但有时候并不是整个页面都要缓存的,如登录的用户名部分,那么如何实现局部不缓存呢? 其实有多种方法,但对于Think ...

  2. python创建有序字典OrderedDict()

    python 有序字典OrderedDict # -*- coding:utf-8 -*- """ python有序字典 需导入模块collections "& ...

  3. 【平面图最小割】BZOJ1001- [BeiJing2006]狼抓兔子

    [题目大意]左上角点为(1,1),右下角点为(N,M)(上图中N=4,M=5).有以下三种类型的道路 1:(x,y)<==>(x+1,y) 2:(x,y)<==>(x,y+1) ...

  4. android实现gif图播放、暂停、继续播放

    之前做过一个项目,在android上实现gif图的播放以及点击屏幕弹出窗口显示gif图片的暂停,之前一直用gifView的jar包实现gif图片的显示,但是在gif暂停.继续播放这块没有找到好的解决方 ...

  5. bzoj 1047 单调队列

    做4次单调队列优化DP. /************************************************************** Problem: 1047 User: idy ...

  6. java知识回顾

    一.构造方法能不能被继承 当然不能,1.构造方法是类的唯一入口 2.构造方法与类名相同 3.子类构造方法中隐式的调用了父类的构造方法 二.值传递和引用传递.不变类和可变类.直接赋值和浅拷贝和深拷贝   ...

  7. html div 宽度随着浏览器自动适应

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Struts2 JSONObject的使用

    一.jar包 使用之前必须引入所须要的jar包,这里包含Struts2和JSONObject各自所必须的 Struts2: commons-fileupload-1.2.1.jarcommons-io ...

  9. XBee Level Shifting

    http://www.faludi.com/bwsn/xbee-level-shifting/ The XBee communication (RX/TX) pins definitely opera ...

  10. 在一台服务器上搭建多个项目的SVN

    需求:一台机子,多个项目,项目之间用户独立不可以相互访问文件 思路:在机子上设置多个代码仓库,用不同的端口号加一区分 实现: 首先安装SVN,我这里使用的是TortoiseSVN 首先创建多个SVN代 ...