CF 1514B AND 0, Sum Big

AND 0, Sum Big

来源:https://codeforces.com/problemset/problem/1514/B
标签:【位运算】【数论】

题目简述

给定两个数n和k,计算出满足长度为n且满足下列条件的数列的数量:
   1.所有的元素都属于[0,2k-1];
   2.所有元素的按位与运算结果为0;
   3.元素和尽可能大;
答案可能很大,对1e9+7取模。

Input
T:测试组数(t<=10)。
每组测试输入一个n,k(1≤n≤105, 1≤k≤20);

Output
输出答案对1e9+7取模的结果

Sample Input

2
2 2
100000 20

Sample Output

4
226732710

More Info

In the first example, the 4 arrays are:
[3,0],
[0,3],
[1,2],
[2,1].

题目思路

做的时候觉得肯定有一个计算的式子,只可惜脑袋太笨没想出来,看了题解竟然是快速幂,有点可惜,可能多试几次就能试出来了。

代码(附注释)

#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
ll n,t,k;

ll ksm(ll a,ll b) //快速幂
{
    ll res=1;
    while(b){
        if(b&1) res=(res*a)%mod;
        a=(a*a)%mod;
        b>>=1;
    }
    return res%mod;
}

int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>t;
    while(t--){
        cin>>n>>k;
        cout<<ksm(n,k)<<endl;
    }

    return 0;
}
posted @ 2021-05-24 09:55  unravel_CAT  阅读(71)  评论(0编辑  收藏  举报