Fibonacci Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1290 Accepted Submission(s): 544
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?
![](https://i-blog.csdnimg.cn/blog_migrate/a7373025d55cb69acd4a2ae9620030d9.jpeg)
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).
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
package acm.hdu;
import java.util.Scanner;
/**
* 解题报告: Fibonacci 前四位可以用数据公式求出:
* 后四位是可以用矩阵快速幂求出,也可以用 Fibonacci 数列的一个性质:
* 尾数循环:
* 斐波那契数列的个位数:一个60步的循环 11235,83145,94370,77415,61785.38190,99875,27965,16730,33695,49325,72910…
* 进一步,斐波那契数列的最后两位数是一个300步的循环,最后三位数是一个1500步的循环,
* 最后四位数是一个15000步的循环,最后五位数是一个150000步的循环。
*
* @author Alex
*
*/
public class Hdu3117_20130603_2015 {
private static int [] fib = new int[40];
static{
fib[0] = 0;
fib[1] = 1;
for(int i = 2; i < fib.length; ++i){
fib[i] = fib[i-1] + fib[i-2];
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
long n = 0;
while(in.hasNextLong()){
n = in.nextLong();
if(n <= 39){
System.out.println(fib[(int) n]);
}else{
solve(n);
}
}
}
private static void solve(long n){
long first = getFirstFour(n);
long last = getLastFour(n);
System.out.printf("%04d...%04d",first,last);
System.out.println();
}
private static long getFirstFour(long n){
double a =n * Math.log10(1 + Math.sqrt(5)) -0.5d*Math.log10(5) - n * Math.log10(2);
a = a - (long)(a);
double result = Math.pow(10, a);
while(result < 1000){
result *= 10;
}
return (long)result;
}
private static long getLastFour(long n){
return getFib(n);
}
private static long getFib(long n){
int [][] fib = new int[][]{{1,1},{1,0}};
int [][] unit = new int[][]{{1,0},{0,1}};
while(n > 0){
if((n&1)==1){
unit = multipyMatrix(unit,fib);
}
fib = multipyMatrix(fib,fib);
n >>= 1;
}
return unit[0][1];
}
private static int [][] multipyMatrix(int [][] a, int [][] b){
int [][] c = new int[a.length][b[0].length];
for(int i = 0; i < a.length; ++i){
for(int j = 0;j < b[0].length; ++j){
for(int k = 0; k < b.length; ++k){
c[i][j] =(c[i][j]+ a[i][k]*b[k][j])%10000;
}
}
}
return c;
}
}