Fibonacci
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 12457 |
|
Accepted: 8851 |
Description
In the Fibonacci integer sequence, f0 = 0, f1 = 1, and fn = F N −1 + Fn −2 for n ≥2. For example, the first ten terms of the Fibonacci sequence is:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
An alternative formula for the Fibonacci sequence is
.
Given an integer n, your goal was to compute the last 4 digits of Fn.
Input
The input test file would contain multiple test cases. Each of the test case consists of a containing n (where 0≤ n ≤1,000,000,000). The end-of-file is denoted by a single line containing the number−1.
Output
For each test case, print the last four digits of Fn. If the last four digits of Fn is all zeros, print ' 0 '; Otherwise, omit any leading zeros (i.e., print Fn mod 10000).
Sample Input
099999999991000000000-1
Sample Output
0346266875
Hint
As a reminder, matrix multiplication is associative, and the product of the A/2x2 matrices is given by
.
Also, note that raising any 2×2 matrix to the 0th power gives the identity matrix:
.
Source
Stanford Local 2006Test instructions: Solving the problem of Fibonacci series; the key to the first question of the Matrix fast power is that the structure of the matrix is fast power after the structure of the Matrix http://www.cnblogs.com/frog112111/archive/2013/05/19/308 7648.html
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <queue>5#include <stack>6#include <map>7 #defineMoD 100008 using namespacestd;9 structMatrixTen { One intm[5][5]; A } ANS,EXM; - - structMatrix Matrix_mulit (structMatrix AA,structmatrix BB) the { - structmatrix there; - for(intI=0;i<2; i++) - { + for(intj=0;j<2; j + +) - { +there.m[i][j]=0; A for(intk=0;k<2; k++) atthere.m[i][j]= (there.m[i][j]+aa.m[i][k]*bb.m[k][j]%mod)%MoD; - } - } - returnthere; - } - intMatrix_quick (intgg) in { -exm.m[0][0]=exm.m[0][1]=exm.m[1][0]=1; toexm.m[1][1]=0; +ans.m[0][0]=ans.m[1][1]=1; -ans.m[0][1]=ans.m[1][0]=0; the while(GG) * { $ if(gg&1) Panax Notoginseng { -ans=Matrix_mulit (ANS,EXM); the } +EXM =matrix_mulit (EXM, EXM); AGG >>=1; the } + returnans.m[0][0]; - } $ intN; $ intMain () - { - while(SCANF ("%d", &n)! =EOF) the { - if(n==-1)Wuyi Break; theprintf"%d\n", Matrix_quick (n)); - } Wu return 0; -}
POJ 3070 Matrix Fast Power