先 感谢大佬的总结
http://blog.youkuaiyun.com/u013795055/article/details/38599321
http://blog.youkuaiyun.com/g_congratulation/article/details/52734306
https://blog.youkuaiyun.com/nyist_tc_lyq/article/details/52981353
然后就没有了
自己打的f(n) = f(n-1)+f(n-2)
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef struct matrix
{
int mat[3][3];
}matrix;
matrix chen(matrix a,matrix b){
int i,j,k;
matrix c;
memset(c.mat,0,sizeof(c.mat));
for(i=0;i<3;i++){
for(k=0;k<3;k++){
for(j=0;j<3;j++){
if(a.mat[i][k]&&b.mat[k][j])
c.mat[i][j] += a.mat[i][k]*b.mat[k][j];
}
}
}
return c;
}
int pow(int n){
int i,j;
matrix ans,e;
ans.mat[0][0] = ans.mat[0][1] = ans.mat[0][2] = 1;
ans.mat[1][0] = ans.mat[2][2] = 1;
ans.mat[1][1] = ans.mat[1][2] = 0;
ans.mat[2][0] = ans.mat[2][1] = 0;
for(i=0;i<3;i++){
for(j=0;j<3;j++)
e.mat[i][j]=(i==j);
}
while(n){
if(n&1) e = chen(e,ans);
ans = chen(ans,ans);
n>>=1;
}
return e.mat[0][0]*3+e.mat[0][1]*2;
}
int main()
{
int n,i,j,ans;
int a[3];
a[1]=2,a[2]=3;
while(cin>>n){
if(n <= 2)
cout<<a[n]<<endl;
else{
ans = pow(n-2);
cout<<ans<<endl;
}
}
return 0;
}