HDU 3117Fibonacci Numbers(求斐波那契前四位与后四位 数论知识+矩阵快速幂)
2013-08-15 15:22
561 查看
Fibonacci Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1387 Accepted Submission(s): 576
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 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).
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-1)+f(n-2)这是它的递推式,他的通项是f(n)=[((1.0+sqrt(5.0))/2)^n
- ((1.0-sqrt(5.0))/2)^n]/sqrt(5). 为什么网上好多博客都中间都写的是加号,真心不解,那样连f(0)=0,f(1)=1,都得不到。
解题思路:前四位容易求。先普及一下知识。
m=375,375=10^2.5740312677277188
a=2,b=0.5740312677277188
log10(m)-(int)log10(m)=(a+b)-a=b
那么10^b是什么东西呢?
实际上代表的是最高位,再乘以10的话就是高二位
然后就是后四位了。给的n是10^8,又是体现快速幂作用的时候到了。
快速幂的思想,可以参考:矩阵的快速幂
AC代码:
#include<iostream> #include<cmath> #include<cstdio> using namespace std; int ret[2][2],tmp[2][2],p[2][2]; int mo=10000,n; //用矩阵快速幂求斐波那契后四位 int first() //求前四位 { double ans; ans=n*log10(0.5+0.5*sqrt(5.0))-0.5*log10(5.0); ans=ans-(int)ans; ans=pow(10.0,ans); while(ans<1000) ans*=10; return (int)ans; } void init() { ret[0][0]=1; ret[0][1]=1; ret[1][0]=1; ret[1][1]=0; p[0][0]=1; p[0][1]=1; p[1][0]=1; p[1][1]=0; } void cal1() { int i,j,k; for(i=0;i<2;i++) for(j=0;j<2;j++) { tmp[i][j]=p[i][j]; p[i][j]=0; } for(i=0;i<2;i++) for(j=0;j<2;j++) for(k=0;k<2;k++) p[i][j]=((p[i][j]+tmp[i][k]*tmp[k][j])%mo+mo)%mo; } void cal2() { int i,j,k; for(i=0;i<2;i++) for(j=0;j<2;j++) { tmp[i][j]=ret[i][j]; ret[i][j]=0; } for(i=0;i<2;i++) for(j=0;j<2;j++) for(k=0;k<2;k++) ret[i][j]=((ret[i][j]+tmp[i][k]*p[k][j])%mo+mo)%mo; } void fastmi() { n-=2; while(n) { if(!(n&1)) { cal1(); n>>=1; } else { cal2(); n--; } } } int main() { int fi[45],i; fi[0]=0,fi[1]=1; for(i=2;i<40;i++) fi[i]=fi[i-1]+fi[i-2]; while(~scanf("%d",&n)) { if(n<40) //不超过8位的直接输出 { printf("%d\n",fi ); continue; } printf("%d...",first()); //计算高四位 init(); fastmi(); //计算低四位 printf("%04d\n",ret[0][0]); } return 0; }
很久以前就想把这个题目A了。。
相关文章推荐
- HDU 5451 Best Solver(矩阵快速幂+ 共轭复数 + 循环节 数论啊 )
- HDU 5015 233 Matrix (数论——矩阵快速幂)
- hdu 5171 GTY's birthday gift(矩阵快速幂,斐波那契)
- hdu 奥运(矩阵快速幂+等比数列快速求和+数论)
- hdu 1588 Gauss Fibonacci(斐波那契,等比数列求和,矩阵快速幂)
- HDU 1568 快速求斐波那契前四位
- hdu 5171 GTY's birthday gift【矩阵快速幂】【思维】【感受矩阵和数论的神奇】
- HDU 4565 So Easy!(共轭复数 + 矩阵快速幂 数论)
- HDU 2256 Problem of Precision(矩阵快速幂 数论 )
- hdu 4990 Reading comprehension 矩阵快速幂or数论 bestcoder round 8b
- HDU 2256 Problem of Precision 数论矩阵快速幂
- HDU 5690 All X 矩阵快速幂
- HDU 5950 Recursive sequence(矩阵快速幂)
- HDU 5171 GTY's birthday gift 矩阵快速幂
- HDU 6185 && 2017广西邀请赛:Covering(矩阵快速幂)
- HDU 5015 233 Matrix(矩阵快速幂)
- HDU 5667 Sequence 【矩阵快速幂】
- hdu 1757 A Simple Math Problem 矩阵优化+快速幂
- hdu 2157 How many ways?? 矩阵快速幂
- hdu 2276矩阵快速幂