Moamen and Ezzat are playing a game. They create an array a of n n n non-negative integers where every element is less than 2 k 2k 2k.
Moamen wins if a 1 & a 2 & a 3 & … & a n ≥ a 1 ⊕ a 2 ⊕ a 3 ⊕ … ⊕ a n a_1\&a_2\&a_3\&…\&a_n≥a_1⊕a_2⊕a_3⊕…⊕a_n a1&a2&a3&…&an≥a1⊕a2⊕a3⊕…⊕an.
Here & denotes the bitwise AND operation, and ⊕ denotes the bitwise XOR operation.
Please calculate the number of winning for Moamen arrays a a a.
As the result may be very large, print the value modulo 1000000007 ( 1 0 9 + 7 ) 1000000007 (10^9+7) 1000000007(109+7).
Input
The first line contains a single integer t ( 1 ≤ t ≤ 5 ) t (1≤t≤5) t(1≤t≤5)— the number of test cases.
Each test case consists of one line containing two integers n n n and k ( 1 ≤ n ≤ 2 ⋅ 1 0 5 , 0 ≤ k ≤ 2 ⋅ 1 0 5 ) k (1≤n≤2⋅10^5, 0≤k≤2⋅10^5) k(1≤n≤2⋅105,0≤k≤2⋅105).
Output
For each test case, print a single value — the number of different arrays that Moamen wins with.
Print the result modulo 1000000007 ( 1 0 9 + 7 ) 1000000007 (10^9+7) 1000000007(109+7).
Example
input
3
3 1
2 1
4 0
output
5
2
1
Note
In the first example, n = 3 , k = 1 n=3, k=1 n=3,k=1. As a result, all the possible arrays are [ 0 , 0 , 0 ] , [ 0 , 0 , 1 ] , [ 0 , 1 , 0 ] , [ 1 , 0 , 0 ] , [ 1 , 1 , 0 ] , [ 0 , 1 , 1 ] , [ 1 , 0 , 1 ] [0,0,0], [0,0,1], [0,1,0], [1,0,0], [1,1,0], [0,1,1], [1,0,1] [0,0,0],[0,0,1],[0,1,0],[1,0,0],[1,1,0],[0,1,1],[1,0,1], and [ 1 , 1 , 1 ] [1,1,1] [1,1,1].
Moamen wins in only 5 5 5 of them: [ 0 , 0 , 0 ] , [ 1 , 1 , 0 ] , [ 0 , 1 , 1 ] , [ 1 , 0 , 1 ] [0,0,0], [1,1,0], [0,1,1], [1,0,1] [0,0,0],[1,1,0],[0,1,1],[1,0,1], and [ 1 , 1 , 1 ] [1,1,1] [1,1,1].
思路
当n为奇数时,二进制下的每一位,都有 C n 0 + C n 2 + . . . + C n n − 1 C_n^0+C_n^{2}+...+C_n^{n-1} Cn0+Cn2+...+Cnn−1 以及全选 C n n C_n^n Cnn 这么多种选法。由杨辉三角的性质,上式等于 2 n − 1 + 1 2^{n-1}+1 2n−1+1。那么结果就是 ( 2 n − 1 + 1 ) k (2^{n-1}+1)^k (2n−1+1)k。
当 n n n 为偶数时,二进制下的每一位,若是全选,那么 n n n 个数剩下的 n × ( k − 1 ) n\times(k-1) n×(k−1) 位二进制的任意组合都满足题意;此外,每一位还有 C n 0 + C n 2 + . . . + C n n − 2 C_n^0+C_n^2+...+C_n^{n-2} Cn0+Cn2+...+Cnn−2 种选择,可化解为 2 n − 1 − 1 2^{n-1}-1 2n−1−1。设 d p [ i ] dp[i] dp[i] 表示 k = 1 k=1 k=1 时的选择数,那么 d p [ i ] = d p [ i − 1 ] × ( 2 n − 1 − 1 ) + 2 ( i − 1 ) × n dp[i]=dp[i-1]\times(2^{n-1}-1)+2^{(i-1)\times n} dp[i]=dp[i−1]×(2n−1−1)+2(i−1)×n。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=1e9+7;
int T,n,k,dp;
int qpow(int a,int b){ int ret=1; while(b){ if(b&1) ret=ret*a%mod; a=a*a%mod,b>>=1; } return ret; }
void solve(){
cin>>n>>k; dp=1;
int x=(qpow(2,n-1)+1)%mod,y=(qpow(2,n-1)+mod-1)%mod;
if(n&1) return (void)(cout<<qpow(x,k)<<"\n");
for(int i=1;i<=k;i++) dp=(y*dp%mod+qpow(2,(i-1)*n))%mod;
cout<<dp<<"\n";
}
signed main(){
ios::sync_with_stdio(false);
for(cin>>T;T;T--) solve();
}
代码种多次用到 2 2 2 的 i i i 次幂,可以不使用快速幂,而是用一些预处理、递推,可以降低复杂度。
今天写代码的时候,把dp=(y*dp%mod+qpow(2,(i-1)*n))%mod;
写成了dp=(y*dp%mod+qpow(2,(i-1)*n%mod))%mod;
(
qpow
\text{qpow}
qpow 中多了个
mod
\text{mod}
mod )。结果一直
w
a
wa
wa ,也不容易找
b
u
g
bug
bug ,后来发现,
2
11
mod
10
≠
2
11
mod
10
mod
10
2^{11}~\text{mod}~10\neq2^{11~\text{mod}~10}~\text{mod}~10
211 mod 10=211 mod 10 mod 10 ,把
mod
\text{mod}
mod 去掉就可以了。