1. 程式人生 > >ACM-ICPC 2018 徐州賽區網絡預賽 A. Hard to prepare

ACM-ICPC 2018 徐州賽區網絡預賽 A. Hard to prepare

lose and 討論 one trigger for out ng- specific

  • 262144K

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.j) and every number has k 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; (T20) .

Next TT lines each contains two numbers, NN and k(0<N,k1e6) .

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

題目來源

ACM-ICPC 2018 徐州賽區網絡預賽

/*
n個人作成一個環,2^k個面具,[0,2^k-1]
每個人一個面具,面具的編號可以相同,相鄰的人的面具的編號
異或結果>0,(1^1==1,0^0==0,0^1==0,和正常的不一樣)問共有幾種排列方式。
*/
/*
分析 :按照題目給的異或,兩個數只有每一位都是不同的,那麽異或的
結果才是0,也就是說,兩者的和為2^k-1,因為(every number has k bits.)
如  :k==2,對於0 :0,1,2都是可以的,只有一個3不行
那麽第一個位置2^k,第二個位置2^k-1,一直到第n-1個位置都是2^k-1
但是第n個位置,要分情況討論了
1:第一個位置和第n-1個位置不同,那麽第n個位置的選擇數目為2^k-2
2:第一個位置和第n-1個位置相同,就不好說了
舉個例子:
n==4,k==2
我們可以先讓整體為2^k*poww(2^k-1,n-2),那麽在poww(2^k-1,n-2)中
包含上面的1,2兩種情況,針對2,少的數目為第一個位置和第N-1個位置
一樣的情況數,因為兩者一樣,可以把兩者看成一個整體,因為2^k-1-(2^k-2)==1
,因此第n個位置是固定的,不需要考慮了,那麽就還剩下n-2個
*/
#define  ull unsigned  long  long 
#define ll  long  long 
const  int N=1e6+4;
const  ll mod=1e9+7;
int t;
ll n,k;
ll f[N];
void  init()
{
    f[0]=1;
    for(int i=1;i<N;i++)
    {
        f[i]=(2ll*f[i-1])%mod;
    }
}
ll poww(ll   a,ll   b)
{
    ll ans=1;
    while(b)
    {
        if(b&1) {
            ans=ans*a%mod;
        }
        b>>=1;
        a=a*a%mod;
    }
    return  ans%mod;
}
ll dfs(int n,int k)
{
    if(n==1) return f[k]%mod;
    else  if(n==2)  return  (f[k]*(f[k]-1))%mod;
    ll ans=0;
    ans=(  (f[k]*poww(f[k]-1,n-2)%mod)*(f[k]-2ll)   )%mod;
    ans=(ans+dfs(n-2,k))%mod;
    return  ans;
}
int main()
{
    
    scanf("%d",&t);
    init();
    while(t--)
    {
        scanf("%lld%lld",&n,&k);
        printf("%lld\n",dfs(n,k));
    }
    return 0;
}

ACM-ICPC 2018 徐州賽區網絡預賽 A. Hard to prepare