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

Next TTT lines each contains two numbers, NNN and k ( 0 < N , k 1 e 6 ) k ( 0 < N , k 1 e 6 ) k ( 0 < N , k 1 e 6 ) .
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 , 2 k 1 ] ,问有多少种不同的序列满足对于所有相邻的两个数字,它们异或值不能为 2 k 1 ,其中第一个数字和最后一个数字也算相邻

分析:

我们仔细分析一下这道题

根据题意我们知道异或取反后的答案只有正数或者0,所以我们只要让他们相邻异或取反不为0即可,异或取反为0说明异或为全1,也就是说明只有两个数的二进制位是互补的时候,才会全一,所以我们只需要避免让二进制互补的两个数相邻即可,而且我们知道对于每个数,与其二进制互补的数只有一个,所以每个位置的选择方案数只需要减去一个和自己互补的那个数即可

首先最一般的情况是,第一个数有 2 k 种选择,第2个数到第n-1个数都有 2 k 1 种选择,第n个数有 2 k 2 种选择

2 k       ,     2 k 1       ,     2 k 1 2 k 1       ,     2 k 1       ,     2 k 2

最一般的情况答案必然是这样的

但是我们知道中间的 2 k 1 仅仅是保证异或取反为正,没有保证其他(比如说有两个位置数字相同),因此我们从局部开始考虑

我们看到如果第n-1个和第1个时相同的第n个位置仅仅只是和一个数不同(就是只需要不等于唯一的一个和它互补的数)即可,也就是应该有 2 k 1 种方案

但是就像刚才说的

2 k       ,     2 k 1       ,     2 k 1 2 k 1       ,     2 k 1       ,     2 k 2
这个式子中仅仅保证了异或取反为正的条件成立,也就是说这种计算方法中也必然包含了第1个位置和第n-1个位置是相同数字的情况,只不过,当他们相同时,最后一个位置应该是 2 k 1 ,而我们呢依然按照 2 k 2 算了,也就是少了一个,那么最后一个位置少了一个,我们想要加上,根据乘法定理,必然是最后一个的那一种方法,乘上前面的所有可能方案,这才能完整的加上少的情况

因此我们需要加上的情况应该是这样的,第一个位置仍然是 2 k 种,那么因为第n-1个位置和第一个位置相同,那么方案就是1,又因为我们这次计算的只是第n位置少的那一种,因此第n个位置也只有一种,因此我们写出来就发现应该是这样的

2 k       ,     2 k 1       ,     2 k 1 2 k 2       ,     1       ,     1

注意因为第n-1个位置和第一个位置相同了,那么第n-2个位置要同时保证,和前一个后一个都不是互补的数才行,因此变成了 2 k 2

因此我们发现除去后面两个1,上面数列变成了长度为n-2的

2 k       ,     2 k 1       ,     2 k 1 2 k 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;
}

猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/82620743