跳到正文

题解

题解:AT_arc108_d [ARC108D] AB

a_small_OIer约 11 分钟
原文
文章信息与版本

文章信息

发布
2026年9月23日
收录
2026年10月13日
更新
2026年11月14日
篇幅
约 11 分钟
获赞
5

版本

  1. v1当前

瞪眼观察法。

神秘分讨题。

模拟赛只获得了暴力分!/hec

AT_arc108_d [ARC108D] AB - Luogu

AT_arc108_d [ARC108D] AB - Atcoder

题意

有初始字符串为 AB。给定四个字符 cAA,cAB,cBA,cBBc_{\mathrm{AA}}, c_{\mathrm{AB}}, c_{\mathrm{BA}}, c_{\mathrm{BB}},每个都是 A 或 B。可以进行任意次操作:若字符串中存在相邻的 XY,则在它们之间插入 cXYc_{\mathrm{XY}}。求经过若干操作后,字符串长度恰好为 nn 时,可能的字符串个数对 109+710^9+7 取模。

思路

观察到任何操作都不会改变字符串的首尾。注意到 cAA,cAB,cBA,cBBc_{\mathrm{AA}}, c_{\mathrm{AB}}, c_{\mathrm{BA}}, c_{\mathrm{BB}} 一共只有 1616 种组合,我们不妨开始大胆分讨。

下面按 cABc_{\mathrm{AB}} 的取值分两大类讨论,下文中使用 X\texttt{X} 表示 “A,B\texttt{A},\texttt{B} 中任何一个均可”的意思,fif_i 表示斐波那契数列的第 ii 位。

当 cAB=Bc_{\mathrm{AB}} = \texttt{B} 时

  • 当 cBB=Bc_{\mathrm{BB}} = \texttt{B} 时,显然字符串最终只会变成 ABBB⋯B\texttt{ABBB}\cdots \texttt{B} 的形式,插入的操作唯一确定,所以答案为 11;
  • cBB=cBA=Ac_{\mathrm{BB}} = c_{\mathrm{BA}} = \texttt{A} 时,字符串最终会变成 ABXXX⋯B\texttt{ABXXX}\cdots \texttt{B} 的形式,中间 n−3n-3 个位置可以是 A,B\texttt{A},\texttt{B} 种任何一个,答案为 2n−32^{n-3};
  • cBB=A,cBA=Bc_{\mathrm{BB}} = \texttt{A},c_{\mathrm{BA}} = \texttt{B} 时,答案为 fn−1f_{n-1}。
证明

当 cAB=Bc_{\mathrm{AB}} = \texttt{B},cBB=Ac_{\mathrm{BB}} = \texttt{A},cBA=Bc_{\mathrm{BA}} = \texttt{B} 时,操作规则为:

  • 相邻对 AB 插入 B;
  • 相邻对 BA 插入 B;
  • 相邻对 BB 插入 A。

显然这样永远不会出现子串 AA。

我们发现所有以 A 开头、以 B 结尾且不含 AA 的字符串都可以被生成。设 dpndp_n 为长度为 nn 的这类字符串数量。考虑长度为 nn 的字符串,其首字符为 A,末字符为 B,且不含 AA。根据倒数第二个字符分类:

  • 若倒数第二个字符为 B,则字符串以 BB 结尾。去掉最后一个 B,得到一个长度为 n−1n-1 的、以 A 开头、以 B 结尾且不含 AA 的字符串。这类字符串有 dpn−1dp_{n-1} 个。
  • 若倒数第二个字符为 A,则字符串以 AB 结尾。由于不含 AA,倒数第三个字符必为 B。去掉末尾的 AB,得到一个长度为 n−2n-2 的、以 A 开头、以 B 结尾且不含 AA 的字符串。这类字符串有 dpn−2dp_{n-2} 个。

因此 dpn=dpn−1+dpn−2dp_n = dp_{n-1} + dp_{n-2}。边界条件为 dp2=1dp_2 = 1(只有 AB),dp3=1dp_3 = 1(只有 ABB)。

于是我们观察发现 dpndp_n 恰好是标准斐波那契数列的第 n−1n-1 项,即 fn−1f_{n-1}(其中 f1=1,f2=1,fn=fn−1+fn−2f_1 = 1, f_2 = 1, f_n = f_{n-1} + f_{n-2})。故答案为 fn−1f_{n-1}。

当 cAB=Ac_{\mathrm{AB}} = \texttt{A} 时

利用对称性将字符 A\mathrm{A} 和 B\mathrm{B} 互换,并将字符串左右反转,操作规则相应互换,答案不变。因此情况与上述一一对应。

  • cAA=Ac_{\mathrm{AA}} = \texttt{A} 时,答案为 11;
  • cAA=cBA=Bc_{\mathrm{AA}} = c_{\mathrm{BA}} = \texttt{B} 时,答案为 2n−32^{n-3};
  • cAA=B,cBA=Ac_{\mathrm{AA}} = \texttt{B},c_{\mathrm{BA}} = \texttt{A} 时,答案为 fn−1f_{n-1}。

复杂度分析

虽然本题 nn 只有 10001000,但是显然我们可以在 O(n)O(n) 时间内计算出所有的 2n−32^{n-3} 和 fn−1f_{n-1}。

更进一步,显而易见的,2n−32^{n-3} 和 fn−1f_{n-1} 都可以做到在 O(log⁡n)O(\log n) 时间内计算,也就是说本题的 nn 开到 101810^{18} 也完全可做。

实现

记得特判 n=2n=2 的情况。

C++
#include <bits/stdc++.h>
#define N 110
using namespace std;
typedef long long LL;
//十年OI一场空,不开long long见祖宗
const int P = 1000000007;
struct Matrix{
	int n , m;
	LL a[N][N];
	inline void clean(){
		memset(a , 0 , sizeof(a));
	}
	inline void build(){
		clean();
		for(int i = 1 ; i <= n ; ++i)
			a[i][i] = 1;
	}
    inline void makefib(){
		clean();
		n = 2;
		m = 2;           
		a[1][1] = a[1][2] = a[2][1] = 1;
    }
	inline void read(int m){
		n = m;
		for(int i = 1 ; i <= n ; ++i)
			for(int j = 1 ; j <= n ; ++j){
				LL x;
				scanf("%lld" , &x);
				a[i][j] = x;
			}
	}
	inline void print(){
		for(int i = 1 ; i <= n ; ++i){
			for(int j = 1 ; j <= m ; ++j)
				printf("%lld " , a[i][j]);
			printf("\n");
		}
	}
    friend Matrix operator *(const Matrix &x , const Matrix &y){
        Matrix res;
        res.clean();
        res.n = x.n;          
        res.m = y.m;             
        for(int i = 1 ; i <= x.n ; ++i)
            for(int j = 1 ; j <= y.m ; ++j)    
                for(int k = 1 ; k <= x.m ; ++k) 
                    res.a[i][j] = (res.a[i][j] + x.a[i][k] * y.a[k][j] % P) % P;
        return res;
    }
    friend Matrix operator %(const Matrix &x , const int &p){
        Matrix res;
        res.n = x.n;
        res.m = x.m;           
        for(int i = 1 ; i <= x.n ; ++i)
            for(int j = 1 ; j <= x.m ; ++j) 
                res.a[i][j] = x.a[i][j] % p;
        return res;
    }
    Matrix qp(LL k){
        Matrix res;
        res.n = n;            
        res.m = m;
        auto &a = *this;
        res.build();
        for(; k ; k >>= 1){
            if(k & 1)
                res = res * a % P;
            a = a * a % P;
        }
        return res;
    }
}matrix;
Matrix fib(){
    Matrix res;
    res.clean();          
    res.n = 2;               
    res.m = 1;               
    res.a[1][1] = 1;
    res.a[2][1] = 0;
    return res;
}
LL fibonacci(LL n){
    Matrix res;
    res.makefib();
    res = res.qp(n - 1);
    res = res * fib();
    return res.a[1][1];
}
LL qp(LL a , LL b){
    LL res = 1;
    for(; b ; b >>= 1){
        if(b & 1)
            res = res * a % P;
        a = a * a % P;
    }
    return res;
}
LL n;
char ab , aa , ba , bb;
int main(){
	//freopen(".in" , "r" , stdin);
	//freopen(".out" , "w" , stdout);
	scanf("%lld" , &n);
    scanf(" %c %c %c %c" , &aa , &ab , &ba , &bb);
    if(n == 2){
        printf("1");
        return 0;
    }
    if(ab == 'A'){
        if(ba == 'A'){
            if(aa == 'A')
                printf("1");
            else
                printf("%lld", fibonacci(n - 1));
        }
        else{
            if(aa == 'A')
                printf("1");
            else printf("%lld", qp(2, n - 3));
        }
    }
    else{ 
        if(bb == 'B')
            printf("1");
        else{
            if(ba == 'A')
                printf("%lld", qp(2, n - 3));
            else
                printf("%lld", fibonacci(n - 1));
        }
    }
    return 0;
}