# HDU6050 Funny Function[矩阵快速幂]

## Funny Function

HDU - 6050

1  1  3  5  11  21  43  85  171  341  683  ......

T_T 观察一下吧

#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=2;
ll a[N][N],b[N][N],ans[N][N],tmp[N][N];
ll Mul(ll a[][N],ll b[][N])
{
memset(tmp,0,sizeof(tmp));
for (int i=0 ; i<N ; ++i)
for (int j=0 ; j<N ; ++j)
if (a[i][j])
for (int k=0 ; k<N ; ++k)
tmp[i][k]=(tmp[i][k]+a[i][j]*b[j][k]%mod+mod)%mod;
for (int i=0 ; i<N ; ++i)
for (int j=0 ; j<N ; ++j)
a[i][j]=tmp[i][j];
}
ll Pow(ll ans[][N],ll a[][N],ll m)
{
while (m)
{
if (m&1)
Mul(ans,a);
Mul(a,a);
m>>=1;
}
}
{
for (int i=0 ; i<N ; ++i)
for (int j=0 ; j<N ; ++j)
a[i][j]=(a[i][j]+flag*b[i][j]+mod)%mod;
}
void initial(ll n,int flag)
{
memset(b,0,sizeof(b));
memset(ans,0,sizeof(ans));
memset(a,0,sizeof(a));
for (int i=0 ; i<N ; ++i)
b[i][i]=ans[i][i]=1;
a[0][1]=a[1][1]=1;
a[1][0]=2;
Pow(ans,a,n);
for (int i=0 ; i<N ; ++i)
for (int j=0 ; j<N ; ++j)
a[i][j]=ans[i][j];
memset(ans,0,sizeof(ans));
for (int i=0 ; i<N ; ++i)
ans[i][i]=1;
if (flag==1)
{
b[0][0]=1;
b[0][1]=-1;
b[1][0]=-2;
b[1][1]=0;
}
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
ll n,m;
scanf("%lld%lld",&n,&m);
int flag=n&1?1:-1;
initial(n,flag);
Pow(ans,a,m-1);
ll res=0;
for (int i=0 ; i<N ; ++i)
res=(res+ans[0][i])%mod;
printf("%lld\n",res);
}
return 0;
}