28 问题分解(递归)

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

  • A. 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 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

题目来源

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

题目的意思是,这里有2的k次方个数值,每一个数字的长度是k,有n个人围成一个圆圈,并且相邻的两个人的数字编号不能同或的0(同或与亦或相反)

               101  

               010

异或值    111

同或值    000

我们如果想要相邻的两个数字同或值不是零的话,只要有一位是相同的就不是零,所以如果有k位的话,那么它的相邻处有2的k次方-1种方案,如果是长度为n的一个圈的话我们很容易想到一共有2^k * (2^k-1)^(n-2) * (2^k - 2) ;我们看到这里分成了三部分第一部分是第一个位置放有2^k种方案,从第二个位置开始到第n-1个位置都有2^k-1种方案,就剩下最后一个位置它的两边都有人所以会有(2^k - 2)种方案这就是上面的式子由来,但是这里我们会发现我们漏掉很多种方案,因为第n-1个位置的人的数字是可能和第一个人的数字是相同的,那么我们公式的第三部分有时候应该不是这样的,我们应该讨论一下就是第1个和第n-1个位置的数字相同和不同时的情况,但是我们又会发现我们要求算1和第n-1个不同又会出现第n-2个位置情况会影响第n-1的数目;

#include <bits/stdc++.h>
using namespace std;

typedef long long ll ;
const int inf = 0x3f3f3f3f;
const int mod =  1e9+7;
const int Max = 1e6+10;
ll n,m,w[Max];

#define rep(i,s,n) for(ll i=s;i<=n;i++)
#define per(i,n,s) for(ll i=n;i>=s;i--)
ll pow(ll base , ll k){
   ll res=(ll)1;
   while(k){
    if(k&1)  res=res*base;
    res=(res%mod+mod)%mod;
    base=base*base;
    base=(base%mod+mod)%mod;
    k>>=1;
   }
   return res;
}
ll de(ll n, ll m){
     ll sum;
     if(n==1) return w[m];

     if(n==2) return (w[m]%mod*(w[m]-1)%mod+mod)%mod;

     ll sum1=(w[m]%mod*pow((w[m]-1),(ll)(n-2))%mod*(ll)max((w[m]-2),(ll)0))%mod;
     ll sum2=de(n-2,m)%mod;
     sum=(sum1+sum2)%mod;
     return (sum%mod+mod)%mod;
}
int main(){
   int t;
   w[0]=1;
   rep(i,1,Max-10){
     w[i]=w[i-1]*2;
     w[i]=(w[i]%mod+mod)%mod;
   }
   scanf("%d",&t);
   while(t--){
    scanf("%lld %lld",&n,&m);
    ll ans=de(n,m);
    printf("%lld\n",ans);
   }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39792342/article/details/82595472
28