Moamen and XOR(dp,位运算,组合数)

链接

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&&ana1a2a3an.

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(1t5)— 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(1n2105,0k2105).

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+...+Cnn1 以及全选 C n n C_n^n Cnn 这么多种选法。由杨辉三角的性质,上式等于 2 n − 1 + 1 2^{n-1}+1 2n1+1。那么结果就是 ( 2 n − 1 + 1 ) k (2^{n-1}+1)^k (2n1+1)k

n n n 为偶数时,二进制下的每一位,若是全选,那么 n n n 个数剩下的 n × ( k − 1 ) n\times(k-1) n×(k1) 位二进制的任意组合都满足题意;此外,每一位还有 C n 0 + C n 2 + . . . + C n n − 2 C_n^0+C_n^2+...+C_n^{n-2} Cn0+Cn2+...+Cnn2 种选择,可化解为 2 n − 1 − 1 2^{n-1}-1 2n11。设 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[i1]×(2n11)+2(i1)×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 去掉就可以了。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_51864047

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值