题解:AT_arc108_d [ARC108D] AB
a_small_OIer · · 题解
瞪眼观察法。
神秘分讨题。
模拟赛只获得了暴力分!/hec
AT_arc108_d [ARC108D] AB - Luogu
AT_arc108_d [ARC108D] AB - Atcoder
题意
有初始字符串为 AB。给定四个字符 A 或 B。可以进行任意次操作:若字符串中存在相邻的 XY,则在它们之间插入
思路
观察到任何操作都不会改变字符串的首尾。注意到
下面按
当 c_{\mathrm{AB}} = \texttt{B} 时
- 当
c_{\mathrm{BB}} = \texttt{B} 时,显然字符串最终只会变成\texttt{ABBB}\cdots \texttt{B} 的形式,插入的操作唯一确定,所以答案为1 ; -
-
::::info[证明]
当
- 相邻对
AB插入B; - 相邻对
BA插入B; - 相邻对
BB插入A。
显然这样永远不会出现子串 AA。
我们发现所有以 A 开头、以 B 结尾且不含 AA 的字符串都可以被生成。设 A,末字符为 B,且不含 AA。根据倒数第二个字符分类:
- 若倒数第二个字符为
B,则字符串以BB结尾。去掉最后一个B,得到一个长度为n-1 的、以A开头、以B结尾且不含AA的字符串。这类字符串有dp_{n-1} 个。 - 若倒数第二个字符为
A,则字符串以AB结尾。由于不含AA,倒数第三个字符必为B。去掉末尾的AB,得到一个长度为n-2 的、以A开头、以B结尾且不含AA的字符串。这类字符串有dp_{n-2} 个。
因此 AB),ABB)。
| 于是我们观察发现 |
|---|
当 c_{\mathrm{AB}} = \texttt{A} 时
利用对称性将字符
复杂度分析
虽然本题
更进一步,显而易见的,
实现
记得特判
#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;
}