POJ3070 Fibonacci
Description
In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
An alternative formula for the Fibonacci sequence is
.
Given an integer n, your goal is to compute the last 4 digits of Fn.
Input
The input test file will contain multiple test cases. Each test case consists of a single line 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 are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).
Sample Input
0 9 999999999 1000000000 -1
Sample Output
0 34 626 6875
Hint
As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by
.
Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:
.
Source
1 //It is made by jump~ 2 #include <iostream> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 #include <algorithm> 8 #include <ctime> 9 #include <vector> 10 #include <queue> 11 #include <map> 12 #include <set> 13 #ifdef WIN32 14 #define OT "%I64d" 15 #else 16 #define OT "%lld" 17 #endif 18 using namespace std; 19 typedef long long LL; 20 const int MOD = 10000; 21 int n; 22 23 struct Fibn{ 24 LL s[5]; 25 }a,b; 26 27 inline int getint() 28 { 29 int w=0,q=0; 30 char c=getchar(); 31 while((c<'0' || c>'9') && c!='-') c=getchar(); 32 if (c=='-') q=1, c=getchar(); 33 while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); 34 return q ? -w : w; 35 } 36 37 inline Fibn cheng(Fibn p,Fibn q){ 38 Fibn tmp; 39 tmp.s[1]=p.s[1]*q.s[1]+p.s[2]*q.s[3]; tmp.s[1]%=MOD; 40 tmp.s[2]=p.s[1]*q.s[2]+p.s[2]*q.s[4]; tmp.s[2]%=MOD; 41 tmp.s[3]=p.s[3]*q.s[1]+p.s[4]*q.s[3]; tmp.s[3]%=MOD; 42 tmp.s[4]=p.s[3]*q.s[2]+p.s[4]*q.s[4]; tmp.s[4]%=MOD; 43 return tmp; 44 } 45 46 inline void fib(){ 47 a.s[1]=1; a.s[2]=0; a.s[3]=0; a.s[4]=1; 48 b.s[1]=0; b.s[2]=1; b.s[3]=1; b.s[4]=1; 49 while(n) { 50 if(n&1) a=cheng(a,b); 51 b=cheng(b,b); 52 n=n/2; 53 } 54 //printf("%d %d %d %d\n",a.s[1],a.s[2],a.s[3],a.s[4]); 55 printf("%d\n",(int)a.s[3]); 56 } 57 58 inline void work(){ 59 while(1) { 60 n=getint(); 61 if(n==-1) break; 62 if(n==0) { printf("0\n"); continue; } 63 fib(); 64 } 65 } 66 67 int main() 68 { 69 work(); 70 return 0; 71 }