1732 Fibonacci Series 2
time limit: 1 sspace limit: 128000 KBtitle level: Diamonds Diamond SolvingTitle Description
Description
In the "1250 Fibonacci series", we find the value of the nth Fibonacci series. But in 1250, n<=109. Now, your task is still to find the value of the nth Fibonacci sequence, but note that n is an integer and 1 <= n <= 100000000000000
Enter a description
Input Description
Enter multiple sets of data, one row for each group of data, and an integer n (1 <= n <= 100000000000000)
Output description
Output Description
Outputs several rows. Output per line (corresponding input) n Fibonacci number (considering the number will be very large, mod 1000000007)
Sample input
Sample Input
3
4
5
Sample output
Sample Output
2
3
5
Data range and Tips
Data Size & Hint
1 <= N <= 100000000000000
Category labels
Tags Click here to expandMatrix multiplication Number Theory
AC Code:
#include <cstdio>#include<cstring>#include<iostream>using namespacestd;#definell Long LongConstll mod=1000000007; ll a[2][2],b[2][2],c[2][2];intMain () {ll n; while(SCANF ("%lld", &n) = =1){ if(n==1) {puts ("1");Continue;} if(n==2) {puts ("1");Continue;} if(n==3) {puts ("2");Continue;} N-=3; a[0][0]=1; a[0][1]=1; a[1][0]=1; a[1][1]=0; b[0][0]=1; b[0][1]=1; b[1][0]=1; b[1][1]=0; while(n) {if(n&1) {c[0][0]= (a[0][0]*b[0][0]%mod+a[0][1]*b[1][0]%MOD)%MoD; c[0][1]= (a[0][0]*b[0][1]%mod+a[0][1]*b[1][1]%MOD)%MoD; c[1][0]= (a[1][0]*b[0][0]%mod+a[1][1]*b[1][0]%MOD)%MoD; c[1][1]= (a[1][0]*b[0][1]%mod+a[1][1]*b[1][1]%MOD)%MoD; memcpy (A,c,sizeofc); } c[0][0]= (b[0][0]*b[0][0]%mod+b[0][1]*b[1][0]%MOD)%MoD; c[0][1]= (b[0][0]*b[0][1]%mod+b[0][1]*b[1][1]%MOD)%MoD; c[1][0]= (b[1][0]*b[0][0]%mod+b[1][1]*b[1][0]%MOD)%MoD; c[1][1]= (b[1][0]*b[0][1]%mod+b[1][1]*b[1][1]%MOD)%MoD; memcpy (B,c,sizeofc); N>>=1; } printf ("%lld\n", (a[0][0]+a[0][1])%MoD); } return 0;}
1732 Fibonacci Series 2