Hard to prepare(2018 ACM-ICPC 徐州赛区网络赛 A)

Problem Description

After Incident, a feast is usually held in Hakurei Shrine. This time Reimu asked Kokoro to deliver a Nogaku show during the feast. To enjoy the show, every audience has to wear a Nogaku mask, and seat around as a circle.

There are N guests Reimu serves. Kokoro has 2^k masks numbered from 0,1,⋯, 2k−1, and every guest wears one of the masks. The masks have dark power of Dark Nogaku, and to prevent guests from being hurt by the power, two guests seating aside must ensure that if their masks are numbered ii and jj , then ii XNOR jj must be positive. (two guests can wear the same mask). XNOR means ~(ii^jj) and every number has kk bits. (1 XNOR 1=1, 0 XNOR 0=1, 1 XNOR 0 = 0)

You may have seen 《A Summer Day's dream》, a doujin Animation of Touhou Project. Things go like the anime, Suika activated her ability, and the feast will loop for infinite times. This really troubles Reimu: to not make her customers feel bored, she must prepare enough numbers of different Nogaku scenes. Reimu find that each time the same guest will seat on the same seat, and She just have to prepare a new scene for a specific mask distribution. Two distribution plans are considered different, if any guest wears different masks.

In order to save faiths for Shrine, Reimu have to calculate that to make guests not bored, how many different Nogaku scenes does Reimu and Kokoro have to prepare. Due to the number may be too large, Reimu only want to get the answer modules 1e9+7 . Reimu did never attend Terakoya, so she doesn't know how to calculate in module. So Reimu wishes you to help her figure out the answer, and she promises that after you succeed she will give you a balloon as a gift.

Input

First line one number T , the number of testcases; (T≤20) .

Next T lines each contains two numbers, N and k(0<N,k≤1e6) .

Output

For each testcase output one line with a single number of scenes Reimu and Kokoro have to prepare, the answer modules 1e9+7 .

Sample Input

2
3 1
4 2

​​​​​​​Sample Output

2
84

题意:现在有 n 个人坐成一圈,每人可以任意选择 0 到 2^k-1 中的一个数,要求任意相邻的两人所选的数字 i、j 的 ~(i^j) 大于 0,问有多少中选择方案

思路:

由于相邻两人所选数字的 ~(i^j) 大于 0,那么也就是说只有当 i^j 后转为二进制数,各位为 1 时才不满足。

可以发现,对于 0 到 2^k 中的数,只有对应的两个数异或后才为全 1,例如:

k=3 时,有:

  • 000  ①
  • 001  ②
  • 010  ③
  • 011  ④
  • 100  ⑤
  • 101  ⑥
  • 110  ⑦
  • 111  ⑧

只有选择 ① 与 ⑧、② 与 ⑦、③ 与 ⑥、④ 与 ⑤ 时,才全为 1

若 n 个人坐成一条链时,设 m=2^k,根据上述的规律,有:

  • 对第一个人,有 m 种选法
  • 对第二个人,只能选择除与第一个人相对应的数外的任意一个数,即有 m-1 种选法
  • 对第三个人,与第二个人同理,也有 m-1 种选法
  • ...
  • 对第 n 个人,同样是有 m-1 种选法

那么总共有 m*(m-1)^(n-1) 种选法

但由于这 n 个人是坐成一个圆,就必须要减去第 n 个人与第 1 个人选择同种数字的情况

若第 n 个人选择的数字与第一个人相同,那么就将这两个人合成一个来看,相当于 n-1 个人去选择数字,设结果为 F(n-1),那么有递推公式:F(n)=m*(m-1)^n-1-F(n-1)

移项化简有:F(n)-(m-1)^n=-(F(n-1)-(m-1)^{n-1}),由此可看出,F(n)-(m-1)^n 是公比为 -1 的等比数列

化简得到:F(n)=(m-1)^n+(m-1)*(-1)^n

于是有:

  • n=1 时:F(1)=m
  • n=2 时:F(2)=m(m-1)
  • n>=3 时:F(n)=(F(2)-(m-1)^{2} )*(-1)^{n-2}+(m-1)^n=(m-1)^n+(m-1)*(-1)^n

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL quickModPow(LL a,LL b,LL mod){ LL res=1; a=a%mod; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1;} return res; }
LL inv(LL a,LL mod){ return quickModPow(a,mod-2,mod); }
const double EPS = 1E-10;
const int MOD = 1E9+7;
const int N = 1E7*2+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        LL n,k;
        scanf("%lld%lld",&n,&k);
        LL m=quickModPow(2,k,MOD);

        if(n==1){
            LL res = m%MOD;
            printf("%lld\n",res);
        }
        else if(n==2){
            LL res = ( m%MOD * (m-1)%MOD )%MOD;
            printf("%lld\n",res);
        }
        else{
            LL temp1 = quickModPow(m-1,n,MOD);
            LL temp2 = ( m%MOD * inv(2,MOD)%MOD - 1 )%MOD;
            LL temp3 = (1 + quickPow(-1,n))%MOD;
            LL res = (temp1%MOD + (temp2%MOD * temp3%MOD) )%MOD+1;
            printf("%lld\n",res);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值