2018ACM-ICPC徐州賽區網絡賽: 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; .
Next TTT lines each contains two numbers, NNN and .
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 徐州賽區網絡預賽
題意:
n個數字,每個數字範圍 ,問有多少種不同的序列滿足對於所有相鄰的兩個數字,它們異或值不能爲 ,其中第一個數字和最後一個數字也算相鄰
分析:
我們仔細分析一下這道題
根據題意我們知道異或取反後的答案只有正數或者0,所以我們只要讓他們相鄰異或取反不爲0即可,異或取反爲0說明異或爲全1,也就是說明只有兩個數的二進制位是互補的時候,纔會全一,所以我們只需要避免讓二進制互補的兩個數相鄰即可,而且我們知道對於每個數,與其二進制互補的數只有一個,所以每個位置的選擇方案數只需要減去一個和自己互補的那個數即可
首先最一般的情況是,第一個數有 種選擇,第2個數到第n-1個數都有 種選擇,第n個數有 種選擇
即
最一般的情況答案必然是這樣的
但是我們知道中間的 僅僅是保證異或取反爲正,沒有保證其他(比如說有兩個位置數字相同),因此我們從局部開始考慮
我們看到如果第n-1個和第1個時相同的第n個位置僅僅只是和一個數不同(就是只需要不等於唯一的一個和它互補的數)即可,也就是應該有 種方案
但是就像剛纔說的
因此我們需要加上的情況應該是這樣的,第一個位置仍然是 種,那麼因爲第n-1個位置和第一個位置相同,那麼方案就是1,又因爲我們這次計算的只是第n位置少的那一種,因此第n個位置也只有一種,因此我們寫出來就發現應該是這樣的
注意因爲第n-1個位置和第一個位置相同了,那麼第n-2個位置要同時保證,和前一個後一個都不是互補的數才行,因此變成了
因此我們發現除去後面兩個1,上面數列變成了長度爲n-2的
所以我們只需要加上這長度n-2的種類數,同理,這個長度下,依然會存在上面說的問題,因此我們發現只需要遞歸求解就可以了,因爲當長度n=1和n=2的時候,答案是唯一確定的
code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
const int maxn = 1e6+10;
ll q_pow(ll a,ll b){
ll ans = 1;
while(b){
if(b & 1)
ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
ll f[maxn];
ll solve(int n,int k){
ll ans;
if(n == 2)
return f[k] * (f[k] - 1) % mod;
if(n == 1)
return f[k];
ans = (f[k] * q_pow(f[k]-1,n-2) % mod * max(f[k]-2,0LL) % mod + solve(n-2,k)) % mod;
return ans;
}
int main(){
int T,n,k;
f[0] = 1;
for(int i = 1; i < maxn; i++){
f[i] = f[i-1] * 2 % mod;
}
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&k);
printf("%lld\n",solve(n,k));
}
return 0;
}