1. 程式人生 > 其它 >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≤210 5 , 0 \le k \le 2\cdot 10^50≤k≤210 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

思路: 

  • 利用位運算的性質,&有一個0就為0,^看1的奇偶個數,因此 將 把n個數 按照二進位制寫就行了,
  • 排在一起,一位一位的看,更具第一點,n為偶數 就有勝利的情況,n為奇數只有平局的情況
  • 偶數: DP就行了, DP[0][i] 代表平局 DP[1][i] 代表勝利。 具體轉移看程式碼比較簡單。
  • 組合數的處理見程式碼,
  • 最後!!!!!!!!!!!!!!!!!!!!!
  • 取mod 一定要看 是不是所有式子都取了mod!!!!!!!!!!!!!!!
  • !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  • 且中間是不是要加%mod,很重要!!!!! 
#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