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

題目鏈接:

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

題意概括:

N 個人圍成一個環,每個人可以選 0-2^{k-1} 之間的一個整數(某個數字可以被重複選),要求是相鄰兩人的數同或後是正數

結果模 1e9 + 7

數據範圍:

T\leq 20

0< N,k\leq 1e6

題解分析:

同或的概念就是對於兩個二進制位,相同爲 1,不同爲 0

由於是無符號數,故兩數同或後不會出現負數。所以只要滿足得到的數不是 0 就可以

若兩數同或後爲 0,說明它們的二進制是完全相反的。因此,對於每個數,有且僅有一個數與它對應,不可以相鄰

有點類似於環上的塗色問題,一開始是這個思路:

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);

要注意取模。還有就是計算方案數 2^{k} 時,數據很大,應該用快速冪取模。

狀態轉移時,兩個數相乘可能會溢出 long long 的範圍,這裏應該用大數相乘取模的方法

傳送門:https://blog.csdn.net/Originum/article/details/81292406

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 徐州賽區網絡預賽

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章