[Codeforces Round #716 (Div. 2)]AND 0, Sum Big(二进制分析,排列组合原理)题解

B. AND 0, Sum Big

time limit per test

2 seconds

memory limit per test

256 megabytes

题目描述

Baby Badawy's first words were "AND 0 SUM BIG", so he decided to solve the following problem. Given two integers n and k, count the number of arrays of length n

such that:

  • all its elements are integers between 0 and 2^{k}-1(inclusive)
  • the bitwise AND of all its elements is 0;
  • the sum of its elements is as large as possible.

Since the answer can be very large, print its remainder when divided by 10^{9}+7.

Input

The first line contains an integer t(1≤t≤10) — the number of test cases you need to solve.

Each test case consists of a line containing two integers n and k (1≤n≤10^{5}, 1≤k≤20).

Output

For each test case, print the number of arrays satisfying the conditions. Since the answer can be very large, print its remainder when divided by 10^{9}+7.

Example

Input

2
2 2
100000 20

Output

4
226732710

Note

In the first example, the 4arrays are:

  • [3,0],
  • [0,3],
  • [1,2],
  • [2,1].

题意

输入n,k。

有长度为n的数组满足下面三个条件:

  • 数组元素要小于等于2^{k}-1,大于等于0
  • 数组内所有元素的类与(and或&)要为0
  • 数组元素总和要最大

注意,元素可以重复

问这样的数组可以找到多少个?

思路

1、数组元素要小于等于2^{k}-1,大于等于0,表明:数组内的每个元素都可以由长度为k的二进制数表示。

2、数组内所有元素的类与(and或&)要为0,说明:由1,我们可以把一维数组看出二维数组(行数为n,列数为k),则由要求2,那么每一列需要且仅需要一个0

3、数组元素总和要最大,所以一列只能有一个零,不然的话,就不会总和最大了。

由1,2,3,=====》要求的数就是n^k。

注意

可能n^k数太大,所以得用快速幂。

AC代码

#include<iostream>
#include<algorithm>
#include<math.h>
typedef long long ll;
using namespace std;
const int N = 1e9+7;
ll n,t,k;
int main(){
    cin>>t;
    while(t--){
        cin>>n>>k;
        ll ans = 1;
        while(k){
            if(k&1){
                ans = (ans*n)%N;
            }
            n = (n*n)%N;
            k>>=1;
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值