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 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到2^k-1,每個人可以選擇一個數字,相鄰兩個人選擇的數字同或值不能為0,問你有多少分配方案
題解:很容易想到,我們可以固定一個位置和倒數第2個位置,則對於這兩個位置夾著的位置有兩種情況,要麼兩邊的數一樣,要麼不一樣,對於不一樣的情況很好處理(詳見程式碼),而對於兩邊相等的情況可以發現其實已經變成了n-2大小的子問題了。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
#define mod 1000000007
ll q[1000005],n,k;
void init()
{
q[0]=1;
for(int i=1;i<=1000002;i++)
q[i]=q[i-1]*2ll%mod;
}
ll qq(ll x,ll y)
{
ll res=1;
while(y)
{
if(y%2)
res=res*x%mod;
x=x*x%mod;
y/=2;
}
return res;
}
ll work(int x)
{
ll res=0;
if(x==1) return q[k];
if(x==2) return q[k]%mod*(q[k]-1)%mod;
res=(q[k]*qq(q[k]-1,x-2)%mod*max(0ll,(q[k]-2))%mod+work(x-2))%mod;
return res;
}
int main(void)
{
init();
int T;
scanf("%d",&T);
while(T--)
{
ll ans=0;
scanf("%lld%lld",&n,&k);
printf("%lld\n",work(n));
}
return 0;
}