[AGC040C] Neither AB nor BA
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 + y \le \frac n2
\]
所以有:
奇数位置上A的数量 + 偶数位置上B的数量\(\le \frac n 2\)
奇数位置上B的数量 + 偶数位置上A的数量\(\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的更多相关文章
- @atcoder - AGC040C@ Neither AB nor BA
目录 @description@ @solution@ @accepted code@ @detail@ @description@ 给定偶数 N,求由 'A', 'B', 'C' 三种字符组成的字符 ...
- 静态链表实现 (A-B)U(B-A)
图中黄色部分为(A-B)U(B-A)的实际意义,用结构数组做静态链表来实现该表达式 大致流程是先建立A链表,接着将挨个输入的B中元素在A链表中遍历.如果没找到,就加到A链表结尾下标为endpointe ...
- 已知 $AB$, 求 $BA$
设 $A,B$ 分别是 $3\times 2$ 和 $2\times 3$ 实矩阵. 若 $\dps{AB=\sex{\ba{ccc} 8&0&-4\\ -\frac{3}{2}& ...
- 矩阵迹 tr(AB)=tr(BA)的证明
其实更为直观的理解是:AB与BA具有相同的对角线元素,因此tr(AB)=tr(BA)必然成立 ref:https://blog.csdn.net/silence1214/article/details ...
- AT5661-[AGC040C]Neither AB nor BA【模型转换】
正题 题目链接:https://www.luogu.com.cn/problem/AT5661 题目大意 一个包含\(A,B,C\)的序列,每次可以选择相邻的两个除了\(AB\)和\(BA\)的删去. ...
- AtCoder Grand Contest 040 C - Neither AB nor BA
传送门 好妙的题啊 首先容易想到简单容斥,统计合法方案数可以考虑总方案数减去不合法方案数 那么先考虑如何判断一个串是否合法,但是直接判断好像很不好搞 这时候就需要一些 $magic$ 了,把所有位置下 ...
- AGC040 Task C. Neither AB Nor BA
Observations 对一个长为 $2N$ 的序列重复下述操作:取走两个相邻且不同的元素.最后能把序列取空的充要条件是序列中不存在出现超过 $N$ 次的元素. 证明:必要性,取 $N$ 次最多能取 ...
- [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 ...
- 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 ...
随机推荐
- 《SVG精髓》笔记(二)
3. 文档结构 在SVG中使用样式(四种方式,可以联想对照HTML样式方法) 内联样式, 直接在标签里设置style属性 <circle cx='20' cy='20' r='10' style ...
- (十九)C语言之指针
- centos6.5和centos7如何搭建php环境(包括php7)
查看下centos的版本信息: #适用于所有的linux lsb_release -a #或者 cat /etc/redhat-release #又或者 rpm -q centos-release 安 ...
- LeetCode 113. 路径总和 II(Path Sum II)
题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22, 5 / ...
- VNC连接Ubuntu 16.04桌面灰色的问题解决
1.安装gnome apt-get install --no-install-recommends ubuntu-desktop gnome-panel gnome-settings-daem ...
- LC 646. Maximum Length of Pair Chain
You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...
- 时间总线框架之EvenBus
概述 EventBus定义:是一个发布 / 订阅的事件总线. 这么说应该包含4个成分:发布者,订阅者,事件,总线. 那么这四者的关系是什么呢? 很明显:订阅者订阅事件到总线,发送者发布事件. 订阅者可 ...
- 数据库开源框架之litepal
主页: [https://github.com/LitePalFramework/LitePal](https://github.com/LitePalFramework/LitePal) 中文文档地 ...
- linux性能监控 + Sendmail 发邮件
sendmail安装 #!/bin/bash#控制发邮件的阈值是在rate,rate1和FF值(三个同样的用途,仅仅是名字不同)##注:该博文中的变量不规范,我是随意定义的,请注意##定义时间倒计时函 ...
- ojdbc15-10.2.0.4.0.jar maven 引用报错 Dependency 'com.oracle:ojdbc15:10.2.0.4.0' not found
ojdbc15-10.2.0.4.0.jar maven 引用报错 问题现象 在 Maven 工程中引用 ojdbc15-10.2.0.4.0.jar 报错,报错信息:Dependency 'com. ...