2018ACM-ICPC徐州賽區網絡賽: A. Hard to prepare(遞推)

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; (T20)(T20)(T20) .

Next TTT lines each contains two numbers, NNN andk(0<N,k1e6)k(0<N,k1e6)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+71e9+7 .
樣例輸入

2
3 1
4 2

樣例輸出

2
84

題目來源

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

題意:

n個數字,每個數字範圍[0,2k1] ,問有多少種不同的序列滿足對於所有相鄰的兩個數字,它們異或值不能爲2k1 ,其中第一個數字和最後一個數字也算相鄰

分析:

我們仔細分析一下這道題

根據題意我們知道異或取反後的答案只有正數或者0,所以我們只要讓他們相鄰異或取反不爲0即可,異或取反爲0說明異或爲全1,也就是說明只有兩個數的二進制位是互補的時候,纔會全一,所以我們只需要避免讓二進制互補的兩個數相鄰即可,而且我們知道對於每個數,與其二進制互補的數只有一個,所以每個位置的選擇方案數只需要減去一個和自己互補的那個數即可

首先最一般的情況是,第一個數有2k 種選擇,第2個數到第n-1個數都有2k1 種選擇,第n個數有2k2 種選擇

2k   ,  2k1   ,  2k12k1   ,  2k1   ,  2k2

最一般的情況答案必然是這樣的

但是我們知道中間的2k1 僅僅是保證異或取反爲正,沒有保證其他(比如說有兩個位置數字相同),因此我們從局部開始考慮

我們看到如果第n-1個和第1個時相同的第n個位置僅僅只是和一個數不同(就是只需要不等於唯一的一個和它互補的數)即可,也就是應該有2k1 種方案

但是就像剛纔說的

2k   ,  2k1   ,  2k12k1   ,  2k1   ,  2k2
這個式子中僅僅保證了異或取反爲正的條件成立,也就是說這種計算方法中也必然包含了第1個位置和第n-1個位置是相同數字的情況,只不過,當他們相同時,最後一個位置應該是2k1 ,而我們呢依然按照2k2 算了,也就是少了一個,那麼最後一個位置少了一個,我們想要加上,根據乘法定理,必然是最後一個的那一種方法,乘上前面的所有可能方案,這才能完整的加上少的情況

因此我們需要加上的情況應該是這樣的,第一個位置仍然是2k 種,那麼因爲第n-1個位置和第一個位置相同,那麼方案就是1,又因爲我們這次計算的只是第n位置少的那一種,因此第n個位置也只有一種,因此我們寫出來就發現應該是這樣的

2k   ,  2k1   ,  2k12k2   ,  1   ,  1

注意因爲第n-1個位置和第一個位置相同了,那麼第n-2個位置要同時保證,和前一個後一個都不是互補的數才行,因此變成了2k2

因此我們發現除去後面兩個1,上面數列變成了長度爲n-2的

2k   ,  2k1   ,  2k12k2

所以我們只需要加上這長度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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章