Description

一个长度为 n 的字符串是好的当且仅当它由 'A', 'B', 'C' 组成,且可以通过若干次删除除了"AB"和"BA"的连续子串变为空串。

问有多少个长度为 n 的好串,对 998244353 取模。

\(n\le 10 ^ 7\) , 保证 n 为偶数。

Solution

本题的关键在于转化题意,即找到一个更加简洁抽象的等价条件方便计数。

连续删除两个字符后发现每一个 A 和 B 的奇偶性没有改变。

这说明了奇数位置的 A 一定不能和偶数位置的 B 消除,偶数位置的 A 不能和奇数位置的B消除。

设奇数位置的 A 有 x 个,偶数位置的 B 有 y 个,偶数位置的非 B 字符有 n - y 个, 那么必须满足(即必要性):

\[x \le \frac{n}{2} - y\\
x + y \le \frac n2
\]

所以有:

  • 奇数位置上A的数量 + 偶数位置上B的数量\(\le \frac n 2\)

  • 奇数位置上B的数量 + 偶数位置上A的数量\(\le \frac n 2\)

必要性显然。

仿照上式子记为:

\[c + d \le \frac n 2 \\
a + b \le \frac n 2
\]

充分性可以考虑先吧序列的 C 都消掉,剩只需考虑 A 和 B 。

由于

\(a + c = b + d = \frac n 2\)

所以

\(a + d = b + c = \frac n 2\)

这样就必然可以找到一对 AA 或 BB 消去。

Thus,\(Ans = 3 ^ n - count(a + b > \frac n 2) - count(c + d > \frac n 2)\) 。

枚举有多少个 a + b (c + d) ,其余的位置就只有两种方案,用组合数算一下即可.

code

#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm> using namespace std; #define End exit(0)
#define LL long long
#define mp make_pair
#define SZ(x) ((int) x.size())
#define GO cerr << "GO" << endl
#define DE(x) cout << #x << " = " << x << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__) void proc_status()
{
freopen("/proc/self/status","r",stdin);
string s; while(getline(cin, s)) if (s[2] == 'P') { cerr << s << endl; return; }
} template<typename T> inline T read()
{
register T x = 0;
register char c; register int f(1);
while (!isdigit(c = getchar())) if (c == '-') f = -1;
while (x = (x << 1) + (x << 3) + (c ^ 48), isdigit(c = getchar()));
return x * f;
} template<typename T> inline bool chkmin(T &a,T b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a,T b) { return a < b ? a = b, 1 : 0; } const int maxN = 1e7 + 2;
const int mod = 998244353; int qpow(int a, int b)
{
int ans = 1;
for (; b; b >>= 1, a = 1ll * a * a % mod)
if (b & 1) ans = 1ll * ans * a % mod;
return ans;
} inline void Inc(int &x) { x < 0 ? x += mod : 0; } int fac[maxN + 2], ifac[maxN + 2], pw2[maxN + 2]; void init(int N = 1e7)
{
fac[0] = 1;
for (int i = 1; i <= N; ++i) fac[i] = (LL) fac[i - 1] * i % mod;
ifac[N] = qpow(fac[N], mod - 2);
for (int i = N - 1; i >= 0; --i) ifac[i] = (LL) ifac[i + 1] * (i + 1) % mod;
pw2[0] = 1;
for (int i = 1; i <= N; ++i) pw2[i] = pw2[i - 1] * 2ll % mod;
} int C(int n, int m)
{
if (n < m) return 0;
return (LL) fac[n] * ifac[m] % mod * ifac[n - m] % mod;
} int n; void input() { n = read<int>(); } void solve()
{
int ans = qpow(3, n);
for (int i = n / 2 + 1; i <= n; ++i)
Inc(ans -= 2ll * C(n, i) * pw2[n - i] % mod);
printf("%d\n", ans);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("xhc.in", "r", stdin);
freopen("xhc.out", "w", stdout);
#endif
input(), init(), solve();
return 0;
}

[AGC040C] Neither AB nor BA的更多相关文章

  1. @atcoder - AGC040C@ Neither AB nor BA

    目录 @description@ @solution@ @accepted code@ @detail@ @description@ 给定偶数 N,求由 'A', 'B', 'C' 三种字符组成的字符 ...

  2. 静态链表实现 (A-B)U(B-A)

    图中黄色部分为(A-B)U(B-A)的实际意义,用结构数组做静态链表来实现该表达式 大致流程是先建立A链表,接着将挨个输入的B中元素在A链表中遍历.如果没找到,就加到A链表结尾下标为endpointe ...

  3. 已知 $AB$, 求 $BA$

    设 $A,B$ 分别是 $3\times 2$ 和 $2\times 3$ 实矩阵. 若 $\dps{AB=\sex{\ba{ccc}  8&0&-4\\  -\frac{3}{2}& ...

  4. 矩阵迹 tr(AB)=tr(BA)的证明

    其实更为直观的理解是:AB与BA具有相同的对角线元素,因此tr(AB)=tr(BA)必然成立 ref:https://blog.csdn.net/silence1214/article/details ...

  5. AT5661-[AGC040C]Neither AB nor BA【模型转换】

    正题 题目链接:https://www.luogu.com.cn/problem/AT5661 题目大意 一个包含\(A,B,C\)的序列,每次可以选择相邻的两个除了\(AB\)和\(BA\)的删去. ...

  6. AtCoder Grand Contest 040 C - Neither AB nor BA

    传送门 好妙的题啊 首先容易想到简单容斥,统计合法方案数可以考虑总方案数减去不合法方案数 那么先考虑如何判断一个串是否合法,但是直接判断好像很不好搞 这时候就需要一些 $magic$ 了,把所有位置下 ...

  7. AGC040 Task C. Neither AB Nor BA

    Observations 对一个长为 $2N$ 的序列重复下述操作:取走两个相邻且不同的元素.最后能把序列取空的充要条件是序列中不存在出现超过 $N$ 次的元素. 证明:必要性,取 $N$ 次最多能取 ...

  8. [ACM_图论] ZOJ 3708 [Density of Power Network 线路密度,a->b=b->a去重]

    The vast power system is the most complicated man-made system and the greatest engineering innovatio ...

  9. Codeforces Round #306 (Div. 2) A. Two Substrings【字符串/判断所给的字符串中是否包含不重叠的“BA” “AB”两个字符串】

    A. Two Substrings time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

随机推荐

  1. TensorFlow使用记录 (十四): Multi-task to MNIST + Fashion MNIST

    前言 后面工作中有个较重要的 task 是将 YOLOV3 目标检测和 LanNet 车道线检测和到一个网络中训练,特别的是,这两部分数据来自于不同的数据源.这和我之前在 caffe 环境下训练检测整 ...

  2. 【知识库】-数据库_MySQL之基本数据查询:子查询、分组查询、模糊查询

    简书作者:seay 文章出处: 关系数据库SQL之基本数据查询:子查询.分组查询.模糊查询 回顾:[知识库]-数据库_MySQL常用SQL语句语法大全示例 Learn [已经过测试校验] 一.简单查询 ...

  3. AJAX-基础-1

    概述 AJAX = Asynchronous JavaScript And XML(异步 JavaScript 及 XML) AJAX 是 Asynchronous JavaScript And XM ...

  4. R-三次指数平滑法实践

    data <- read.csv("H://day_shuaka.csv") raw0 <- data[359:752,] raw0$weekday <- as. ...

  5. HNOI2012排队

    排列组合题(本文A(n,m)表示从n个元素里选m个的排列数). 首先,老师和女生有不能相邻的限制条件,应该用插空法.而且老师人数较少且固定,把老师和男生进行混合,对女生用插空. 我先来一手错误做法,n ...

  6. django CBV模式源码执行过程

    在说CBV模式之前,先看下FBV的url配置方式: urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^xxx/', login), ur ...

  7. D建立app项目(mui)

    参考 http://dev.dcloud.net.cn/mui/getting-started/ 1.ios需要下载iTunes,确保手机能连上电脑 2.mui可参考手册 http://dev.dcl ...

  8. Async Task Types in C#

    Async Task Types in C# Extend async to support task types that match a specific pattern, in addition ...

  9. Linux搭建PHP环境(LAMP)

    //安装Apache的命令 # yum install httpd //启动Apache的命令 # service httpd start //安装MySQL的命令 # wget http://dev ...

  10. Helm chart仓库官方仓库不能使用解决方法

    Helm chart仓库官方仓库不能使用解决方法 k8s中的官方helm chart仓库在国内可能使用不了,但是我们又需要使用,这里推荐几个方法. 使用其他的chart仓库 微软的chart仓库 ht ...