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

简介: 题意:用 < 2k的数填充到长度为n的数组中,要使得数组中所有数& >= ^,问方案数显然,当k == 1的时候,答案为1,只有当所有的数全为1的情况才可以满足题意对于其他的情况{用小于2k的数进行填充,那么说明填充的数字的二进制位数最多可以有k kk位,从数的个数的角度来说,奇数个1^ 之后的结果是1,偶数个1^ 之后的结果是0,而对于0来讲,不管多少个0,^起来结果都是0只要有一个0,那么说这一位上&之后就是0,而为了让^为0,那么只能够选择偶数个1如果某一位上&为1,那么^为1的情况需要讨论n的奇偶性

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 kk位,

从数的个数的角度来说,奇数个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



目录
相关文章
|
存储 缓存 数据库
Django框架学习(四)
Django框架学习(四)
|
负载均衡
如何手写一个简单的分布式系统框架?
如何手写一个简单的分布式系统框架?
271 0
|
数据库
LeetCode(数据库)- The Number of Seniors and Juniors to Join the Company II
LeetCode(数据库)- The Number of Seniors and Juniors to Join the Company II
164 0
Latex单词首字母大写
关注九天学者微信公众号(扫码关注)第一时间获取技术贴更新! 宏包 \usepackage{mfirstuc} 全部大写 \capitalisewords{Numerical simulation of circulation mechanism o...
4412 0
|
3天前
|
弹性计算 人工智能 安全
云上十五年——「弹性计算十五周年」系列客户故事(第二期)
阿里云弹性计算十五年深耕,以第九代ECS g9i实例引领算力革新。携手海尔三翼鸟、小鹏汽车、微帧科技等企业,实现性能跃升与成本优化,赋能AI、物联网、智能驾驶等前沿场景,共绘云端增长新图景。
|
9天前
|
存储 弹性计算 人工智能
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
2025年9月24日,阿里云弹性计算团队多位产品、技术专家及服务器团队技术专家共同在【2025云栖大会】现场带来了《通用计算产品发布与行业实践》的专场论坛,本论坛聚焦弹性计算多款通用算力产品发布。同时,ECS云服务器安全能力、资源售卖模式、计算AI助手等用户体验关键环节也宣布升级,让用云更简单、更智能。海尔三翼鸟云服务负责人刘建锋先生作为特邀嘉宾,莅临现场分享了关于阿里云ECS g9i推动AIoT平台的场景落地实践。
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
|
8天前
|
人工智能 自然语言处理 自动驾驶
关于举办首届全国大学生“启真问智”人工智能模型&智能体大赛决赛的通知
关于举办首届全国大学生“启真问智”人工智能模型&智能体大赛决赛的通知
|
8天前
|
云安全 人工智能 自然语言处理
阿里云x硅基流动:AI安全护栏助力构建可信模型生态
阿里云AI安全护栏:大模型的“智能过滤系统”。