[容斥原理]Hard to prepare

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^k2k masks numbered from 0,1,\cdots,0,1,⋯, 2^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 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. (11 XNOR 1 = 11=1, 00XNOR 0 = 10=1, 11 XNOR 0 = 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+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 TT , the number of testcases; (T \le 20)(T≤20) .

Next TT lines each contains two numbers, NNand 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+7 .

样例输入复制

1
2
3
2
3 1
4 2

样例输出复制

1
2
2
84

题目来源

ACM-ICPC 2018 徐州赛区网络预赛

题目描述

有n个位置围成一个圈,2^k个数,每个位置随便放置一个数,要求范围在1到2^k之间,然而每个数都有一个互斥的数,且互斥的两个数不会和其他的重复,要求相邻位置不能有互斥的数。

题解

首先用快速幂求出2^k然后赋给k,即k = 2^k。然后假设这n个数依次排开,那么每一个数不能是上一个元素的互斥数,那么第1个元素共k种,那么之后每一种为k-1种。即k*(k-1)^(n-1),然而这样的话最后一个元素可以为第一个元素的互斥数,因此要减去相应的数,根据容斥原理最终列出实在为k*(k-1)^(n-1)-k*(k-1)^(n-2)+k*(k-1)^(n-3)-k*(k-1)^(n-4)……然而这样做会超时,因此我们可以合并,即加一次减一次合并到一起重新得到公式。

当然我们也可以换一种思考方式,很容易想到,第1个数有\small 2^k种选择,第2个数到第n-1个数都有\small 2^k-1种选择,第n个数有\small 2^k-2种选择。

所以答案就是\small 2^k(2^k-2)(2^k-1)^{n-2}

但是这样会出现漏算:在第1个数和第n-1个数相同的情况下,第n个数有\small 2^k-1种选择, 而并非\small 2^k-2

然后仔细分析可以发现,漏算的情况你可以把第1个数和第n-1个数当成同一个数,这样序列长度就变成n-2了,问题规模变小

递归或递推即可

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = 1e9+7;
ll quick_pow(ll ans, ll res, ll n){
while(n){
if(n&1)
ans = ans * res % mod;
res = res * res % mod;
n>>=1;
}
return ans;
}
ll solve(ll n, ll k){
ll ans = 0;
ll i = (n&1)+2;
ll res = pow(k-1,i-2);
for(;i<=n;i+=2){
ans = (ans + res) % mod;
res = res * (k - 1) % mod * (k - 1) % mod;
}
ans = ans * k % mod * (k - 2) % mod;
ans = (ans + k) % mod;
return ans;
}
int main(){
ll t,n,k;
scanf("%lld",&t);
while(t--){
scanf("%lld%lld",&n,&k);
k = quick_pow(1,2,k);
printf("%lld\n",solve(n,k));
}
return 0;
}

纸短情长啊文章结束了但我们的故事还在继续
坚持原创技术分享,您的支持将鼓励我继续创作!