[Codeforces] 1557 C Moamen and XOR(组合数学)

这篇博客探讨了一种编程竞赛问题,涉及到在长度为n的数组中填充小于2k的非负整数,使得位运算符AND的值不小于位运算符XOR的值。博主详细分析了当k等于1时的特殊情况,以及对于其他k值时如何计算满足条件的数组数量。通过位运算的性质,博主给出了解决方案,并提供了代码实现。文章最后附带了一个相关的编程题目和样例输出。
摘要由CSDN通过智能技术生成

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).

Example
input

3
3 1
2 1
4 0

output

5
2
1

Note
In the first example, n=3, k=1. As a result, all the possible arrays are [0,0,0], [0,0,1], [0,1,0], [1,0,0], [1,1,0], [0,1,1], [1,0,1], and [1,1,1].

Moamen wins in only 5 of them: [0,0,0], [1,1,0], [0,1,1], [1,0,1], and [1,1,1].

题意:
用 < 2k的数填充到长度为n的数组中,要使得数组中所有数& >= ^,问方案数

显然,当k == 1的时候,答案为1,只有当所有的数全为1的情况才可以满足题意
对于其他的情况{
用小于2k的数进行填充,那么说明填充的数字的二进制位数最多可以有 k k k位,
从数的个数的角度来说,奇数个1^ 之后的结果是1,偶数个1^ 之后的结果是0,而对于0来讲,不管多少个0,^起来结果都是0
只要有一个0,那么说这一位上&之后就是0,而为了让^为0,那么只能够选择偶数个1
如果某一位上&为1,那么^为1的情况需要讨论n的奇偶性
{
当n为奇数,全1时&的结果是1,^的结果也是1
当n为偶数,全1时&的结果为1,^运算结果为0,这时显然这一位已经大于异或了,由于后面的权值小,所以可以任意选择
}
}
Code:

ll fact[maxn], infact[maxn];
void init() {
    fact[0] = infact[0] = 1;
    for (int i = 1; i <= 200000; i++) {
        fact[i]   = fact[i - 1] * i % mod;
        infact[i] = infact[i - 1] * qPow(i, mod - 2) % mod;
    }
}
ll C(ll n, ll m) {
    return fact[n] * infact[n - m] % mod * infact[m] % mod;
}
ll n, m;
int main() {
    init();
    int T = read;
    while (T--) {
        ll ans = 0;
        n = read, m = read;
        if (m == 0) {
            puts("1");
            continue;
        }
        if (n % 2) {
            ans = qPow((qPow(2, n - 1) + 1) % mod, m);
            /**
            ll t = 1;
            for (int i = 0; i <= n - 1; i += 2) {
 				t = (t + C(n,i)) % mod;
            }
            ans = qPow(t,m);
            **/
        } else {
            ll t = 0;
            /**
            for (int i = 0; i <= n - 2; i += 2) {
                t = (t + C(n, i)) % mod;
            }
            ans = qPow(t, m);
            **/
            t   = qPow(2, n - 1) - 1;
            ans = qPow(t, m);
            for (int i = 1; i <= m; i++) {
                ans = (ans + qPow(t, i - 1) * qPow(2, n * (m - i))) % mod;
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}

推荐博客:https://blog.csdn.net/zhouzi2018/article/details/119601507

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值