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 徐州赛区网络预赛

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