# ACM-ICPC 2018 徐州賽區網絡預賽 A. Hard to prepare [ DP + 快速冪 + 環上組合 ]

### 題目鏈接：

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

## 題意概括：

N 個人圍成一個環，每個人可以選  之間的一個整數(某個數字可以被重複選)，要求是相鄰兩人的數同或後是正數

## 題解分析：

https://wenku.baidu.com/view/3ac5bf20b0717fd5360cdcb8.html

long long dp[N][3]
• dp[n][0] 表示共有 n 人時，首尾數字相同的方案數
• dp[n][1] 表示共有 n 人時，首尾數字相反的方案數
• dp[n][2] 表示共有 n 人時，首尾數字既不相同也不相反的方案數

dp[i][0] = dp[i - 1][0] + dp[i - 1][2];
dp[i][1] = dp[i - 1][1] + dp[i - 1][2];
dp[i][2] = dp[i - 1][0] * (m - 2) + dp[i - 1][1] * (m - 2) + dp[i - 1][2] * (m - 3);

## AC代碼：

#include <stdio.h>
#include <memory.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 10;
ll dp[MAXN][3];

ll mod_mult(ll a, ll b, ll mod) {
a %= mod;
b %= mod;
ll ans = 0;
while (b > 0) {
if (b & 1) {
ans += a;
if (ans >= mod)
ans -= mod;
}
a <<= 1;
if (a >= mod) a = a - mod;
b >>= 1;
}
return ans;
}

ll mod_pow(ll x, ll n, ll mod) {
if (n == 0) return 1;
ll res = mod_pow(mod_mult(x, x, mod), n / 2, mod);
if (n & 1) res = mod_mult(res, x, mod);
return res;
}

int main () {
int T;
scanf("%d", &T);
while(T--) {
int N, k;
scanf("%d%d", &N, &k);

ll m = mod_pow(2, k, MOD);
dp[1][0] = m; dp[1][1] = 0; dp[1][2] = 0;
dp[2][0] = m; dp[2][1] = 0; dp[2][2] = mod_mult(m, m - 2, MOD);

for (int i = 3; i <= N; i++) {
dp[i][0] = (dp[i - 1][0] + dp[i - 1][2]) % MOD;
dp[i][1] = (dp[i - 1][1] + dp[i - 1][2]) % MOD;
dp[i][2] = (mod_mult(dp[i - 1][0], (m - 2), MOD) + mod_mult(dp[i - 1][1], (m - 2), MOD) + mod_mult(dp[i - 1][2], (m - 3), MOD)) % MOD;
}
printf("%lld\n", (dp[N][0] + dp[N][2]) % MOD);
}
}

# 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 2k masks numbered from 0, 1, ⋯ , 2k − 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 i and j , then i XNOR j must be positive. (two guests can wear the same mask). XNOR means ~(i^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; (T ≤ 20) .
Next T lines each contains two numbers, N and 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 + 7 .

### 樣例輸⼊

2
3 1
4 2

### 樣例輸出

2
84

### 題目來源

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