hdu3117之矩阵快速幂
来源:互联网 发布:西北大学软件学院 编辑:程序博客网 时间:2024/05/29 05:05
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?
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 108 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).
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
0123453536373839406465
Sample Output
0112359227465149303522415781739088169632459861023...41551061...77231716...7565
重点在于如何求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 99999999using 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之矩阵快速幂
- 快速矩阵幂HDU3117
- hdu3117 Fibonacci Numbers (矩阵快速幂)
- HDU3117-Fibonacci Numbers(矩阵快速幂+log)
- HDU3117 Fibonacci Numbers(矩阵快速幂)
- hdu3117(斐波那契数列+矩阵快速幂)
- HDU3117(快速幂)
- hdu 1568求斐波那契数的前4位 && hdu3117矩阵快速幂
- hdu3117
- hdu3117
- hdu3117
- hdu2157之矩阵快速幂
- hdu4549之矩阵快速幂
- HDU3893之矩阵快速幂
- hdu4291之矩阵快速幂
- hdu4565之矩阵快速幂
- hdu2154之矩阵快速幂
- hdu2371之矩阵快速幂
- Page Redirect Speed Test
- hdu Questions and answers
- 对称子字符串的最大长度
- proc文件目录学习
- iOS: UIScrollView的indicatorStyle常量
- hdu3117之矩阵快速幂
- 《启示录》读书笔记系列--笔记三
- after the first ten days
- iOS: UIScrollView的Deceleration常量
- 1701. Ostap and Partners(带权值并查集)
- 刚子扯谈:微信 今天你打飞机了嘛吗?
- 设置MyEclipse默认打开JSP文件的方式
- LVS 三种工作模式的优缺点比较
- Base64简单说明