• 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

     后记:

    • 当答案错误时,自己可以输一些大数据,看看他对不对(出现乱码就是不对) 通过这个方法来判读mod是不是取完整了的
  • 相关阅读:
    用Github发布静态页面
    JS实现图片放大查看
    CSS3-字体渐变色
    Eclipse切换工作空间(Workspace)
    JS打开新的窗口
    HTML中特殊符号的处理
    PHP转码函数
    SecureCRT按退格键出现^H问题解决
    商人过河问题(二)java实现
    商人过河问题(一)
  • 原文地址:https://www.cnblogs.com/Lamboofhome/p/16170404.html
Copyright © 2020-2023  润新知