题解:AT_arc108_d [ARC108D] AB

· · 题解

瞪眼观察法。

神秘分讨题。

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

AT_arc108_d [ARC108D] AB - Luogu

AT_arc108_d [ARC108D] AB - Atcoder

题意

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

思路

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

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

当 c_{\mathrm{AB}} = \texttt{B} 时

::::info[证明] 当 c_{\mathrm{AB}} = \texttt{B},c_{\mathrm{BB}} = \texttt{A},c_{\mathrm{BA}} = \texttt{B} 时,操作规则为:

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

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

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

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

当 c_{\mathrm{AB}} = \texttt{A} 时

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

复杂度分析

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

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

实现

记得特判 n=2 的情况。

#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;
}