求:
F(m,1)
公式:
(F(m,1)F(m,1))=(F(1,1)F(1,2))∗(An−B0)m−1n为偶数时
(F(m,1)F(m,1))=(F(1,1)F(1,2))∗(An−B1)m−1n为奇数时
其中
A=(0121)B0=(1001)B1=(−1120)
证明参看大佬博客
直接矩阵快速幂求解
code:
#include<iostream>
#include"string.h"
#include<stdio.h>
using namespace std;
const int bc=2;
const int mod = 1000000007;
struct matrix
{
long long x[bc][bc];
};
matrix mutimatrix(matrix a,matrix b)
{
matrix temp;
memset(temp.x,0,sizeof(temp.x));
for(int i=0;i<bc;i++) //答案的行
{
for(int j=0;j<bc;j++) //答案的列
{
for(int k=0;k<bc;k++)
{
temp.x[i][j]+=a.x[i][k]*b.x[k][j];
temp.x[i][j]%=mod;
temp.x[i][j]+=mod;
temp.x[i][j]%=mod;
}
}
}
return temp;
}
matrix sub(matrix a,matrix b)
{
matrix temp;
memset(temp.x,0,sizeof(temp.x));
for(int i=0;i<bc;i++) //答案的行
{
for(int j=0;j<bc;j++) //答案的列
{
temp.x[i][j]=a.x[i][j]-b.x[i][j];
temp.x[i][j]%=mod;
temp.x[i][j]+=mod;
temp.x[i][j]%=mod;
}
}
return temp;
}
matrix powmatrix(matrix a,long long b)
{
matrix temp;
memset(temp.x,0,sizeof(temp.x));
//初始化矩阵为单位阵
for(int i=0;i<bc;i++)
temp.x[i][i]=1;
while(b)
{
if(b%2==1)
temp=mutimatrix(temp,a);
a=mutimatrix(a,a);
b=b/2;
}
return temp;
}
int main()
{
int kase;
scanf("%d",&kase);
long long n;
long long m;
while(kase--)
{
cin>>n>>m;
matrix a,b;
if(n%(2LL)==0)
{
b.x[0][0]=1;b.x[0][1]=0;
b.x[1][0]=0;b.x[1][1]=1;
}
else{
b.x[0][0]=-1;b.x[0][1]=2;
b.x[1][0]=1;b.x[1][1]=0;
}
a.x[0][0]=0;a.x[0][1]=2;
a.x[1][0]=1;a.x[1][1]=1;
matrix temp=powmatrix(a,n);
temp=sub(temp,b);
matrix res=powmatrix(temp,m-1);
long long ans=res.x[0][0]+res.x[1][0];
ans%=mod;
ans+=mod;
ans%=mod;
cout<<ans<<endl;
}
return 0;
}