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. 洛谷P2707 Facer帮父亲 [优先队列,数学]

    题目传送门 Facer帮父亲 题目背景 Facer可是一个孝顺的孩纸呦 题目描述 Facer的父亲是一名经理,现在总是垂头丧气的. Facer问父亲,怎么啦?父亲说,公司出了点问题啊. 公司管理着N个 ...

  2. Android 最基础生命周期及旋转屏幕问题

    public class MainActivity extends Activity { private static final String TAG ="MainActivity&quo ...

  3. 机器学习之路: python 线性回归LinearRegression, 随机参数回归SGDRegressor 预测波士顿房价

    python3学习使用api 线性回归,和 随机参数回归 git: https://github.com/linyi0604/MachineLearning from sklearn.datasets ...

  4. JQ中get()与eq()的区别

    .eq() : 减少匹配元素的集合,根据index索引值,精确指定索引对象. .get() : 通过检索匹配jQuery对象得到对应的DOM元素. 同样是返回元素,那么eq与get有什么区别呢? eq ...

  5. 推荐C#网站、书籍、资源

    推荐博客: 极简的随笔 http://www.cnblogs.com/guwei4037/p/3499135.html 见证大牛成长之路的专栏 http://blog.csdn.net/shanyon ...

  6. tomcat-调整内存参数

    查看Tomcat的默认内存参数: <% /; /; /; out.println("max="+max); out.println("total="+to ...

  7. 我们为什么需要Map-Reduce?

    在讨论我们是否真的需要Map-Reduce这一分布式计算技术之前,我们先面对一个问题,这可以为我们讨论这个问题提供一个直观的背景. 问题 我们先从最直接和直观的方式出发,来尝试解决这个问题: 先伪一下 ...

  8. offset大家族(一)

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. sass compass问题小结

    1.中文注释编译报错Invalid GBK character的问题,找到ruby sass安装目录,如D:\Ruby22-x64\lib\ruby\gems\2.2.0\gems\sass-3.4. ...

  10. 白光LED驱动方案的选择 TPS61043

    所有专为驱动白光LED而设计的IC都提供恒定电流夕其中尽大多数是基于电感或电荷泵的解决方案9这两种解决方案各有其优缺点. 电荷泵解决方案也称为开关电容器解决方案,利用分离电容器将电源从输进端传送至输出 ...