图片转自:http://www.cnblogs.com/huxianglin/p/5995649.html
代码作为模板。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MOD=1e9+7;
ll n;
struct Matrix {
int v[2][2];
Matrix() {memset(v,0,sizeof(v));}
}un,t;
Matrix mult(Matrix a,Matrix b) {
Matrix ret;
for (int k=0;k<2;++k)
for (int i=0;i<2;++i)
for (int j=0;j<2;++j)
ret.v[i][j]=(ret.v[i][j]+1ll*a.v[i][k]*b.v[k][j]%MOD)%MOD;
return ret;
}
int main() {
scanf("%lld",&n);
un.v[0][0]=un.v[1][1]=1;
t.v[0][0]=t.v[0][1]=t.v[1][0]=1;
while (n) {
if (n&1) un=mult(un,t);
t=mult(t,t),n>>=1;
}
printf("%lld\n",un.v[1][0]);
return 0;
}