0
点赞
收藏
分享

微信扫一扫

C. Moamen and XOR (组合数学)


C. Moamen and XOR
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Moamen and Ezzat are playing a game. They create an array a of n non-negative integers where every element is less than 2k.

Moamen wins if 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.

As the result may be very large, print the value modulo 1000000007 (109+7).

Input
The first line contains a single integer t (1≤t≤5)— the number of test cases.

Each test case consists of one line containing two integers n and 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 (109+7).

题解:对于异或运算的结果是0还是1,我们只需要关注1的奇偶就行,奇数个1异或结果是1,偶数个1异或结果为0(多少个0异或结果都是0)。

对于某一位,不是全1的情况,显然与运算结果是0,这时为了保证异或运算不大于与运算必须是异或运算也为0,即必须满足有偶数个1。(这里排列组合计算就行)。

但是对于n个数的每一位单独看,这一位上全1的情况需要分n的奇偶讨论,当n为奇数全1时此时与的结果是1,异或的结果也是1,这一位依然是相等的跟上面讨论的不全1的情况一样。
当n为偶数时,全1时与运算结果为1,异或运算结果为0,这时显然这一位已经大于异或了,后面的哪些位随便选就行。

PS:这题当时比赛WA了有十发了,最后才发现奇数偶数要特判。。。。

AC代码:

#include <iostream>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#define int long long
using namespace std;
const int N=2e5+5,mod=1e9+7;
int n,m;
int fact[N],infact[N];
int qmi(int a,int k){
int res=1;
while(k){
if(k&1)res=res*a%mod;
a=a*a%mod;
k>>=1;
}
return res%mod;
}

int C(int a,int b){
return fact[a]%mod*infact[a-b]%mod*infact[b]%mod;
}

void solve(){
cin>>n>>m;

int res=0;
if(n%2){
int sum=1;
for(int i=0;i<=n-1;i+=2)sum=(sum+C(n,i))%mod;
res=qmi(sum,m);
}
else{
int sum=0;
for(int i=0;i<=n-2;i+=2)sum=(sum+C(n,i))%mod;
res=qmi(sum,m);

for(int i=1;i<=m;i++)
res=(res+qmi(sum,i-1)*qmi(2,n*(m-i)))%mod;;
}

cout<<res<<endl;
}

main(){
fact[0]=infact[0]=1;
for(int i=1;i<N;i++){
fact[i]=fact[i-1]*i%mod;
infact[i]=infact[i-1]*qmi(i,mod-2)%mod;
}

int T;
cin>>T;
while(T--)solve();
}


举报

相关推荐

0 条评论