其他分享
首页 > 其他分享> > Moamen and XOR (位运算+组合数+取模+逆元+dp)

Moamen and XOR (位运算+组合数+取模+逆元+dp)

作者:互联网

Moamen and Ezzat are playing a game. They create an array aa of nn non-negative integers where every element is less than 2^k2 
k
 .

Moamen wins if a_1 \,\&\, a_2 \,\&\, a_3 \,\&\, \ldots \,\&\, a_n \ge a_1 \oplus a_2 \oplus a_3 \oplus \ldots \oplus a_na 
1
​
 &a 
2
​
 &a 
3
​
 &…&a 
n
​
 ≥a 
1
​
 ⊕a 
2
​
 ⊕a 
3
​
 ⊕…⊕a 
n
​
 .

Here \&& denotes the bitwise AND operation, and \oplus⊕ 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 1\,000\,000\,0071000000007 (10^9 + 710 
9
 +7).

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

Each test case consists of one line containing two integers nn and kk (1 \le n\le 2\cdot 10^51≤n≤2⋅10 
5
 , 0 \le k \le 2\cdot 10^50≤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 1\,000\,000\,0071000000007 (10^9 + 710 
9
 +7).

Sample 1
Inputcopy    Outputcopy
3
3 1
2 1
4 0
5
2
1
Note
In the first example, n = 3n=3, k = 1k=1. As a result, all the possible arrays are [0,0,0][0,0,0], [0,0,1][0,0,1], [0,1,0][0,1,0], [1,0,0][1,0,0], [1,1,0][1,1,0], [0,1,1][0,1,1], [1,0,1][1,0,1], and [1,1,1][1,1,1].

Moamen wins in only 55 of them: [0,0,0][0,0,0], [1,1,0][1,1,0], [0,1,1][0,1,1], [1,0,1][1,0,1], and [1,1,1][1,1,1].
View problem

思路: 

#include <bits/stdc++.h>
using namespace std;
#define ri register int 
#define M 200005

template <class G> void read(G &x)
{
    x=0;int f=0;char ch=getchar();
    while(ch<'0'||ch>'9'){f|=ch=='-';ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    x=f?-x:x;
    return ;
}
const int mod=1e9+7;
long long inv[M],inf[M];
long long al[M];
int n,m;
long long ksn(long long a,int n)
{
    long long ans=1;
    while(n)
    {
        if(n&1) ans=ans*a%mod;
        n>>=1;a=a*a%mod;
    }
    return ans;
}
void init()
{
    inv[0]=inf[0]=1;
    for(ri i=1;i<=2e5;i++)
    {
        inf[i]=inf[i-1]*i%mod;
        inv[i]=inv[i-1]*ksn(i,mod-2)%mod;
    }
    al[0]=1;
    for(ri i=1;i<=2e5;i++)
    {
        al[i]=al[i-1]*2%mod;
    }
}
long long dp[4][M];

long long zh(int n,int b)
{
    return inf[n]*inv[n-b]%mod*inv[b]%mod;
}
void sol1()
{
   long long pin=0;
   for(ri i=0;i<n;i+=2)
   {
        pin=(pin+zh(n,i))%mod;
   }    
   
   dp[0][1]=pin;
   dp[1][1]=1;
   dp[2][1]=1;
   for(ri i=2;i<=m;i++)
   {
         dp[2][i]=al[n]*dp[2][i-1]%mod; // 这个点胜利 其他点 随便取 
         dp[1][i]=dp[2][i];
         dp[1][i]=(dp[1][i]+pin*(dp[1][i-1])%mod)%mod;
         dp[0][i]=pin*dp[0][i-1]%mod;
   }
   long long ans=(dp[0][m]+dp[1][m])%mod;
   printf("%lld\n",ans);
}
void sol2()
{
   long long pin=0;
   for(ri i=0;i<n;i+=2)
   {
        pin=(pin+zh(n,i))%mod;
   }
   pin++;long long ans=1; 
   for(ri i=1;i<=m;i++)
   {
        ans=ans*pin%mod;
   }
   printf("%lld\n",ans);
}
int main(){
    
    init();
    int T;
    read(T);
    while(T--)
    {
        read(n);read(m);
        if(m==0)
        {
            printf("1\n");continue;
        }
        if(n&1) sol2();
        else sol1(); 
    }
    return 0;
}
 
View Code

 

标签:取模,ch,XOR,10,Moamen,le,oplus,mod
来源: https://www.cnblogs.com/Lamboofhome/p/16170404.html