zoukankan      html  css  js  c++  java
  • 计蒜客 31453

    题目链接:https://nanti.jisuanke.com/t/31453

    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 $2^k$ masks numbered from $0,1,cdots, 2^k - 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 test cases; $(T le 20)$.

    Next $T$ lines each contains two numbers, $N$ and $k(0<N, k le 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$.

    题意:

    有 $n$ 个人围成一圈,并且有 $2^k$ 个数($0$ 到 $2^k - 1$),每个人可以选择一个数(可以选择一样的数),

    要求:假设任意相邻的两个人的数字为 $i$ 和 $j$,必须满足 $i$ XNOR $j > 0$(XNOR代表同或),

    请给出这 $n$ 个人挑选数字的方案数(答案 $mod 1e9 + 7$)。

    题解:

    首先所有数字都为正,所以按位同或的结果必然为非负的,那么考虑同或结果不是正数的——同或等于 $0$,

    我们知道按位同或,只要有一位一样(都为 $0$ 或者都为 $1$),就不会等于零,所以对于任意一个数 $i$,有且仅有一个数 $j = sim i$ 使得 $i$ XNOR $j = 0$($sim$ 代表按位取反)。

    记$m = 2^k$,那么,显然第 $1$ 个人有 $m$ 种选择,第 $2$ 个人到第 $n-1$ 个人都有 $m-1$ 种选择,第 $n$ 个人只有 $m-2$ 种选择,此时方案数为 $mleft( {m - 1} ight)^{n - 2} left( {m - 2} ight)$;

    但显然有漏算,因为第 $n$ 个人只有 $m-2$ 种选择是他自以为只有 $m-2$ 种选择,而实际上存在一些个情况使得他可以有 $m-1$ 种选择,那什么时候可以比原来多一个选择呢?

    显然,就是当第 $1$ 个人和第 $n-1$ 个人选择了相同的数字的时候,第 $n$ 个人可以多一个选择,而第 $n$ 个人多一个选择,就相应的多了一个方案

    换句话说,第 $1$ 个人和第 $n-1$ 个人选择了相同的数字的情况有多少种,就再加上去几个方案。

    那么第 $1$ 个人和第 $n-1$ 个人选择了相同的数字的情况有多少种呢?

    由于第 $1$ 个人和第 $n-1$ 个人永远选择相同的数字,可以把这两个左右端点等效成一个点,

    那么就从第 $1$ 个人到第 $n-1$ 个人的链型问题变成了第 $1$ 个人到第 $n-2$ 个人的一个环形问题了,

    也就是要求,$n-2$ 个人围成一圈,每个人从 $m$ 个数字里选择一个,满足相邻数字同或为正的条件下,有多少种选择方案。

    这一看,不就是题目的要求吗,无非是从 $n$ 个人变成了 $n-2$ 个人罢了,所以,我们可以用递归的方式求解。

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    
    const ll MOD=1e9+7;
    ll fpow(ll a,ll b)
    {
        ll r=1,base=a%MOD;
        while(b)
        {
            if(b&1) r*=base,r%=MOD;
            base*=base;
            base%=MOD;
            b>>=1;
        }
        return r;
    }
    
    int n;
    ll m,k;
    
    ll f(int n)
    {
        if(n==1) return m % MOD;
        if(n==2) return m * (m-1) % MOD;
        return ( m * fpow(m-1,n-2) % MOD * (m-2) % MOD + f(n-2) % MOD ) % MOD;
    }
    
    int main()
    {
        int T;
        for(cin>>T;T;T--)
        {
            cin>>n>>k;
            m=fpow(2,k);
            cout<<f(n)<<endl;
        }
    }
  • 相关阅读:
    ARC下的内存泄露
    iPhone 被同步到 Mac上后 如果不希望更新到Mac上在哪里删除?
    图像处理之基础---2个YUV视频 拼接技术
    多媒体开发之---H264 RTSP交互过程
    6410开发之---uboot
    数学和算法之---重温傅立叶
    多媒体开发之---h264 rtp打包
    多线程开发之---线程等待
    多媒体开发之---开源库ffmeg的log之子解析
    直升机的革命---鱼鹰运输机
  • 原文地址:https://www.cnblogs.com/dilthey/p/9664383.html
Copyright © 2011-2022 走看看