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m1$F_{m,1}=F_{1,1}\cdot (A^n-B_1)^{m-1}$

A=[1120]

n偶数
B=[1001]

n奇数
B=[0121]

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long ll;
const ll P = 1000000007;
const int N=2;
ll n,m;

struct matrix{
ll a[N][N];
int row,col;
matrix():row(N),col(N){memset(a,0,sizeof(a));}
matrix(int x,int y):row(x),col(y){memset(a,0,sizeof(a));}
ll* operator [] (int x){return a[x];}
matrix operator * (matrix x){
matrix tmp ;
for (int i=0;i<=n+1;i++)
for (int j=0;j<=n+1;j++){
tmp[i][j]=0;
for (int k=0;k<=n+1;k++)
tmp[i][j]=(tmp[i][j]+a[i][k]*x[k][j])%P;
}
return tmp;
}
matrix operator - (matrix x){
matrix tmp ;
for (int i=0;i<=n+1;i++)
for (int j=0;j<=n+1;j++){
tmp[i][j]=a[i][j] - x[i][j] + P;
tmp[i][j]%=P;
}
return tmp;
}
void operator *= (matrix x){*this = *this * x;}
matrix operator ^ (ll x){
matrix ret;
for (int i=0;i<=n+1;i++)ret[i][i]=1;
matrix tmp = *this;
for (;x;x>>=1,tmp*=tmp){if(x&1)ret *=tmp;}
return ret;
}
};

int main(){
long long T,nn,mm;
cin>>T;
n=0;
while(T--){
scanf("%lld%lld",&nn,&mm);
matrix A,B,ans;
A[0][0]=1;A[0][1]=2;A[1][0]=1;A[1][1]=0;
if(nn&1){
B[0][0]=0;B[0][1]=2;
B[1][0]=1;B[1][1]=-1;
} else {
B[0][0]=1;B[0][1]=0;
B[1][0]=0;B[1][1]=1;
}
ans = ((A ^ nn) - B) ^ (mm-1);
cout<<(ans[0][0])<<endl;
}
return 0;
}

Fm,1=(2n1)m123|n$F_{m,1}=\frac{(2^n-1)^{m-1}\cdot 2}{3}|n为偶数$
Fm,1=(2n1)m12+13|n$F_{m,1}=\frac{(2^n-1)^{m-1}\cdot 2 + 1}{3}|n为奇数$

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

typedef long long ll;

const ll P = 3000000021LL;
const ll MOD = 1000000007;

ll pow(ll a,ll n){
if(n==0) return 1LL;
ll tmp = pow(a,n/2);
if(n&1)
return tmp*tmp%P*a%P;
return tmp*tmp%P;
}

int main(){
int T;
ll n,m;
cin>>T;
while(T--){
scanf("%lld%lld",&n,&m);
ll t = pow(2LL,n);
t = (t - 1) % P;
t = pow(t,m-1);
if(n&1) t = t * 2LL + 1;
else t = t * 2LL;
t /= 3LL;
cout<<t%MOD<<endl;
}
return 0;
}

个人资料
等级：
访问量： 2万+
积分： 720
排名： 7万+