1. 程式人生 > >51Nod——T 1242 斐波那契數列的第N項

51Nod——T 1242 斐波那契數列的第N項

input getchar nod 技術 += long mod .html sta

https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1242

基準時間限制:1 秒 空間限制:131072 KB 分值: 0 難度:基礎題 技術分享 收藏 技術分享 關註 斐波那契數列的定義如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ...) 給出n,求F(n),由於結果很大,輸出F(n) % 1000000009的結果即可。 Input
輸入1個數n(1 <= n <= 10^18)。
Output
輸出F(n) % 1000000009的結果。
Input示例
11
Output示例
89

 1 #include <cstdio>
 2 
 3 const int mod(1000000009);
 4 #define LL long long
 5 inline void read(LL &x)
 6 {
 7     x=0; register char ch=getchar();
 8     for(; ch>9||ch<0; ) ch=getchar();
 9     for(; ch>=0&&ch<=
9; ch=getchar()) x=x*10+ch-0; 10 } 11 LL n; 12 13 struct Matrix { 14 LL e[2][2]; 15 Matrix operator * (const Matrix x) const 16 { 17 Matrix tmp; 18 for(int i=0; i<2; ++i) 19 for(int j=0; j<2; ++j) 20 { 21 tmp.e[i][j]=0
; 22 for(int k=0; k<2; ++k) 23 tmp.e[i][j]+=e[i][k]*x.e[k][j],tmp.e[i][j]%=mod; 24 } 25 return tmp; 26 } 27 }ans,base; 28 29 int Presist() 30 { 31 read(n); 32 ans.e[0][0]=ans.e[1][1]=1; 33 base.e[0][0]=base.e[0][1]=base.e[1][0]=1; 34 for(; n; n>>=1, base=base*base) 35 if(n&1) ans=ans*base; 36 printf("%lld",ans.e[0][1]); 37 return 0; 38 } 39 40 int Aptal=Presist(); 41 int main(){;}

51Nod——T 1242 斐波那契數列的第N項