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, 00 XNOR 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, NN and 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 .
样例输入复制
2 3 1 4 2
样例输出复制
2 84
题目来源
编辑代码
题意:
n个位置一圈,每个位置放一个数,相邻两个数不能异或为二进制位置下全1,数有0→2^(k−1),可重复选,求方案数(位置不同也算)
分析:
做法1:
对于k=2的时候来说:
00
01
10
11
00与11不能在一起,01与10不能在一起,也就是一个数只有一个数不能在它旁边。
- 所以对于第一个点来说,他会有m种选择(m=2^k)
- 对于第二个点来说,他会有m-1种选择
- ……
- 对于第n-1个点和第n个点来说,需要分两种情况:
- 第1个点与第n-1个点的数不同,则n-1点有m-1种情况,n点有m-2种情况
- 第1个点与第n-1个点的数相同,则这种情况下肯定第n-1个点肯定会影响到n-2个点,相当于去求1到n-2满足题意的情况(可以想象把1和n-1合并出一个,即又走一遍n-2的过程。
所以公式:ans[n,m]=m*(m-1)^(n-2)*(m-2)+ans[n-2,m]
做法2:
上面像极了圆染色问题:https://wenku.baidu.com/view/3ac5bf20b0717fd5360cdcb8.html
圆n个部分k种颜色,有(-1)^n*(k-1)+(k-1)^n(n>=2)选择,
这题代表可以发现奇数的时候可以ans=(-1)^n*(k-1)+(k-1)^n(n>=2)+2^k
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
typedef long long LL;
const LL MOD=1e9+7;
LL pow_mod(LL a, LL n, LL m)
{
long long ans = 1;
while(n)
{
if(n&1)
{
ans = (ans * a) % m;
}
a = (a * a) % m;
n >>= 1;
}
return ans;
}
LL work(LL n,LL m)
{
if(n==1)
{
return m%MOD;
}
else if(n==2)
{
return m*(m-1)%MOD;
}
else
{
LL ans=0;
LL temp=1;
if(n&1) {temp=-1;ans=m%MOD;}
//cout<<((temp%MOD)*((m-1)%MOD))%MOD<<endl;
ans=(ans+(((temp%MOD)*((m-1)%MOD))%MOD+pow_mod(m-1,n,MOD)+MOD)%MOD+MOD)%MOD;
return ans;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
LL n,k;
scanf("%lld%lld",&n,&k);
LL m=pow_mod(2,k,MOD);
cout<<work(n,m)<<endl;
}
return 0;
}
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
typedef long long LL;
const LL MOD=1e9+7;
LL pow_mod(LL a, LL n, LL m)
{
long long ans = 1;
while(n)
{
if(n&1)
{
ans = (ans * a) % m;
}
a = (a * a) % m;
n >>= 1;
}
return ans;
}
LL work(LL n,LL m)
{
if(n==1)
{
return m%MOD;
}
else if(n==2)
{
return m*(m-1)%MOD;
}
else
{
return (((m%MOD*pow_mod(m-1,n-2,MOD))%MOD*(m-2)%MOD)%MOD+work(n-2,m)%MOD)%MOD;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
LL n,k;
scanf("%lld%lld",&n,&k);
LL m=pow_mod(2,k,MOD);
cout<<work(n,m)<<endl;
}
return 0;
}