推出n为奇偶时各自的公式,
偶数:f(m,1)=[(2^n-1)^(m-1)]/3
奇数:f(m,1)=(2*(2^n-1)^(m-1)+1)/3
然后快速幂,除三注意逆元,用费马小定理就够了
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int t;
LL n,m;
#define mod 1000000007
LL quick(LL a, LL p)
{
LL ans=1;
a%=mod;
while(p)
{
if(p&1) ans=ans*a%mod;
p>>=1;
a=a*a%mod;
}
return ans%mod;
}
int main()
{
cin>>t;
while(t--)
{
cin>>n>>m;
if(n%2==0)
{
LL ans=quick(quick(2,n)-1,m-1)*2*quick(3,mod-2);
ans%=mod;
cout<<ans<<endl;
}
else
{
LL ans=(quick(quick(2,n)-1,m-1)*2+1)*(quick(3,mod-2));
ans%=mod;
cout<<ans<<endl;
}
}
}