Codeforces Round #737 (Div. 2)—C. Moamen and XOR
Moamen and Ezzat are playing a game. They create an array aa of nn non-negative integers where every element is less than 2^k.
Moamen wins if a1&a2&a3&…&an≥a1⊕a2⊕a3⊕…⊕ana1&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 aa.
As the result may be very large, print the value modulo 1000000007(10e9+7).
Input
The first line contains a single integer tt (1≤t≤5)— the number of test cases.
Each test case consists of one line containing two integers nn and kk (1≤n≤2⋅10^5, 0≤k≤2⋅10^5).
Output
For each test case, print a single value — the number of different arrays that Moamen wins with.
Print the result modulo 1000000007(10e9+7).
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <unordered_map>
#define ll long long
using namespace std;
const int mod=1e9+7;
ll ksm(ll a,ll b){
ll ans=1,base=a;
while(b){
if(b&1)ans=ans*base%mod;
base=base*base%mod;
b>>=1;
}
return ans%mod;
}
int main()
{
int t;scanf("%d",&t);
while(t--){
ll n,k;scanf("%lld %lld",&n,&k);
ll c=ksm(2,n-1);//每一位相等有多少可能,偶数个1,异或值为0
//先计算相等的情况
if(n%2)c=(c+1)%mod;//如果n是奇数,那么存在一种异或值为1的情况,全1
else c=(c-1+mod)%mod;//如果n是偶数,那么n个数为1的情况不是相等的情况
ll ans=ksm(c,k);
if(n%2==0){//只有偶数时存在大于的情况
for(int i=1;i<=k;i++){//枚举大于的情况,要大于,枚举每一位上与操作为1(全1),异或操作为0的情况
//当前位右边随便
ll r=ksm(2,(k-i)*n);
//当前位左边要相等
ll l=ksm(c,i-1);
//当前位全为1
ans=(ans+l*r%mod)%mod;
//不会重复是因为,左边的位要全部相等,而左边全部相等,左边的每一位必不可能全为1
}
}
printf("%lld\n",ans);
}
return 0;
}