△-> 3*△ +1*▽
▽-> 1*△ +3*▽
所以 a[i][0]=a[i-1][0]*3+a[i-1][1];
a[i][1]=a[i-1][0]+a[i-1][1]*3
构造个矩阵
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define cler(arr, val) memset(arr, val, sizeof(arr))
typedef long long LL;
const int MAXN = 191;
const int MAXM = 11111;
const int INF = 0x3f3f3f3f;
const LL mod =1000000007;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
LL n;
struct mat
{
LL s[4][4];
};
void operator*=( mat &a,const mat &b)
{
static mat c;
int i,j,k;
memset(c.s,0,sizeof(c));
for(k=0; k<2; k++)
for(i=0; i<2; i++)
for(j=0; j<2; j++)
c.s[i][j]+=a.s[i][k]*b.s[k][j]%mod,c.s[i][j]%=mod;
for(i=0; i<2; i++)
for(j=0; j<2; j++)
a.s[i][j]=c.s[i][j];
}
mat ans,t;
int main()
{
cin>>n;
t.s[0][0]=3;
t.s[0][1]=1;
t.s[1][0]=1;
t.s[1][1]=3;
cler(ans.s,0);
ans.s[0][0]=ans.s[1][1]=1;
for(LL i=n; i; i=i>>1,t*=t)
if(i&1) ans*=t;
cout<<ans.s[0][0]<<endl;
return 0;
}
△-> 3*△ +1*▽
△-> 3*△ +1*▽