A. Hard to prepare

版权声明:小白一个,欢迎各位指错。 https://blog.csdn.net/qq_36424540/article/details/82586694

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 2k2^k2k masks numbered from 0,1,⋯,0,1,\cdots,0,1,⋯, 2k−12^k - 12k−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 iii and jjj , then iii XNOR jjj must be positive. (two guests can wear the same mask). XNOR means ~(iii^jjj) and every number has kkk bits. (111 XNOR 1=11 = 11=1, 000 XNOR 0=10 = 10=1, 111 XNOR 0=00 = 00=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+71e9+71e9+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 TTT , the number of testcases; (T≤20)(T \le 20)(T≤20) .

Next TTT lines each contains two numbers, NNN and k(0<N,k≤1e6)k(0<N, k \le 1e6)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+71e9+71e9+7 .

样例输入

2
3 1
4 2

样例输出

2
84

联想 + 推理

我们假设有 n 颗珠子,   我们不考虑开头 对末尾的限制, 只是考虑 i(i+1)的限制,答案就是(2^{K})(2^{K}-1)^{N-1} , 这些情况是多了的,多了  A...\sim A 这种情况, 然后我们再来计算这种情况有多少?

现在的条件 是 已知 第一个是 A, 最后一个是\sim A, 问一共有多少种方案。

我们先考虑简单的情况 ,假设一共只有 3个 珠子  ,我们第一个是A,第三个是\sim A, 那么第二个只能选(2^{k}-2) 个。 所以多了的就是2^{k}*(2^{k}-2) 中情况。

然后推广一下, 我们考虑  n 这个位置是 \sim A,那么 (n-1)  这个位置 只有  2^{k}-2  种情况, 所以我们减掉 2^{k}*(2^{k}-1)^{n-3}*(2^{k}-2) 种情况,可是这个情况仍然 少了  (n-2) 这个位置上 是\sim A 的情况,所以我们还得加上 现在确定 第一个位置是  A ,第 (n-2) 个位置是 \sim A 的情况, 所以一直 .......   , 结束的位置在哪里呢?

结束的位置也就是 最早出现 \sim A的位置,也就是  第三个位置,所以 我们可以变得位置计算到 第二个也就是  可以了

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)
const int mod=1e9+7;

LL pow_mod(LL base,LL n,int mod) {
	LL ans=1;
	while(n) {
		if(n&1)ans=ans*base%mod;
		base=base*base%mod;
		n>>=1;
	}
	return ans%mod;
}

int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		LL N,K;
		scanf("%lld %lld",&N,&K);
		LL ans=0;
		LL t1=pow_mod(2,K,mod);
		LL t2=(t1-1+mod)%mod;
		LL t3=(t1-2+mod)%mod;

		LL sum=t1;
		for(int i=2; i<=N; i++)sum=sum*t2%mod;
		ans=sum;

		LL inv_t2=pow_mod(t2,mod-2,mod);
		//LL inv_
		//	printf("ans:%lld\n",ans);
		for(int i=N-1; i>=1; --i) {
			sum=sum*inv_t2%mod;
			if((N-i)%2==0) {
				LL tmp=sum*t3%mod;
				ans=(ans-tmp+mod)%mod;
				//	printf("i:%d ans:%lld sum:%lld tmp:%lld\n",i,ans,sum,tmp);
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/82586694