3 条题解

  • 2
    @ 2023-12-24 16:37:20

    这个题目是一个数学题,打表可以发现答案是……

    好了,言归正传。

    下文设 XKX_K 为字符串 XXN=KN=K 时的值,字符串间的 ++C++/STL/string 中的字符串拼接。

    ANA_NBNB_N 中与 CNC_N 匹配次数较少的字符串为 GNG_N(另一个为 HNH_N),则 mc(GN,CN)=3N12\operatorname{mc}(G_{N},C_{N})=\dfrac{3^N-1}{2},且 GNG_NCNC_N 的最后一位一定不同。

    证明:

    首先,N=1N=1 时显然成立,即样例 11

    接下来,从 NN 推到 N+1N+1

    下面的性质很显然,证明不给了。

    AN+1=AN+BN+ANA_{N+1}=A_N+B_N+A_N

    BN+1=BN+AN+BNB_{N+1}=B_N+A_N+B_N

    CN+1=CN+CN+DNC_{N+1}=C_N+C_N+D_NDND_NCNC_N 改变最后一位,即 1\texttt{1} 变成 0\texttt{0}0\texttt{0} 变成 1\texttt{1} 后的字符串)。

    可以发现,此时 $\operatorname{mc}(A_{N+1},C_{N+1})=\operatorname{mc}(A_{N},C_{N})+\operatorname{mc}(B_{N},C_{N})+\operatorname{mc}(A_{N},D_{N})$,$\operatorname{mc}(B_{N+1},C_{N+1})=\operatorname{mc}(B_{N},C_{N})+\operatorname{mc}(A_{N},C_{N})+\operatorname{mc}(B_{N},D_{N})$。

    mc(GN,CN)=3N12\operatorname{mc}(G_{N},C_{N})=\dfrac{3^N-1}{2},且 GNG_NCNC_N 的最后一位不同,则 mc(GN,DN)=3N+12\operatorname{mc}(G_{N},D_{N})=\dfrac{3^N+1}{2}

    而由于当 GNG_NHNH_N 「合在一起」(每个位置两个字符)时,每位各出现了一个 0\texttt{0} 和一个 1\texttt{1}(必有一个和 DND_N 对应位置匹配),因此 $\operatorname{mc}(G_{N},D_{N})+\operatorname{mc}(H_{N},D_{N})=3^N$。

    也就是说,mc(HN,DN)=3N12\operatorname{mc}(H_{N},D_{N})=\dfrac{3^N-1}{2}

    它们之间差 11,因此 mc(AN+1,CN+1)\operatorname{mc}(A_{N+1},C_{N+1})mc(BN+1,CN+1)\operatorname{mc}(B_{N+1},C_{N+1}) 之间也差 11(其余的抵消了)。

    同理,$\operatorname{mc}(G_{N+1},C_{N+1})+\operatorname{mc}(H_{N+1},C_{N+1})=3^{N+1}$。

    因此,$\operatorname{mc}(G_{N+1},C_{N+1})=\dfrac{3^{N+1}-1}{2}$(列方程组可得)。

    此外,由于 $\operatorname{mc}(H_{N},D_{N})<\operatorname{mc}(G_{N},D_{N})$,因此 mc(GN+1,CN+1)\operatorname{mc}(G_{N+1},C_{N+1}) 的展开式(指「可以发现」那一句中的公式)中的最后一项是 mc(HN,DN)\operatorname{mc}(H_{N},D_{N})

    而由于 DND_NCNC_NCNC_NGNG_NGNG_NHNH_N 的最后一位都不同,因此 DND_NHNH_N 的最后一位也不同,即 GN+1G_{N+1}CN+1C_{N+1} 的最后一位也不同。

    根据数学归纳法,得证。

    也就是说,答案就是 3N12\dfrac{3^N-1}{2}

    std:

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    inline ll read() {
    	ll x = 0, f = 1; char ch = getchar();
    	while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); }
    	while(ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }
    	return x * f;
    }
    const ll mod = 1000000007;
    ll T;
    inline ll power(ll a, ll b) {
    	ll ans = 1;
    	while(b) {
    		if(b & 1) ans = (ans * a) % mod;
    		a = (a * a) % mod;
    		b >>= 1;
    	}
    	return ans;
    }
    signed main() {
        T = read();
        while(T--) {
            ll x = read(); x %= mod - 1;
            ll a = power(3, x) - 1;
            printf("%lld\n", ((a & 1) ? a + mod : a) / 2);
        }
        return 0;
    }
    
    • 1
      @ 2024-1-21 14:10:20

      打表大法好啊:

      #include<bits/stdc++.h>
      using namespace std;
      const long long mod=1e9+7;
      int power(long long a,long long b){
          long long ans=1;
          while(b){
              if(b&1){
                  ans=ans*a%mod;
              }
              a=a*a%mod;
              b>>=1;
          }
          return ans;
      
      }
      int main(){
          int T;
          cin>>T;
          while (T--) {
              long long n;
              cin>>n;
              long long a=power(3,n)-1;
              cout<<((a & 1) ? a + mod : a) / 2<<endl;
          }
      }
      
      • 0
        @ 2024-1-1 21:48:36

        Solution

        给一个找规律的解法。

        我们可以先用程序将 [1,5][1,5] 区间内所有的答案打出来,结果为:

        1,4,13,40,1211,4,13,40,121

        n=4n=4 举例,发现 40=30+31+32+3340 = 3 ^ 0 + 3 ^ 1 + 3 ^ 2 + 3 ^ 3

        \therefore 答案便是 30+31++3n13^0+3^1+\dots+3^{n-1}

        那如何快速求出答案呢,下面进行推导。

        S=30+31++3n1S = 3^0+3^1+\dots+3^{n-1},则 3S=31+32++3n3S = 3^1+3^2+\dots+3^n

        $\because 2S = 3S - S = (3^1+3^2+\dots+3^n) - (3^0+3^1+\dots+3^{n-1})$ =3n1=3^{n}-1

        S=2S2=3n12\therefore S = \dfrac{2S}{2}= \dfrac{3^{n}-1}{2}

        推导过后,整个题目便变得明朗起来了。直接快速幂求出 3n3^{n} 后套公式即可。

        • 1

        信息

        ID
        6
        时间
        500ms
        内存
        512MiB
        难度
        4
        标签
        (无)
        递交数
        10
        已通过
        6
        上传者