[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 ...
随机推荐
- CDOJ 1133 菲波拉契数制 变直接统计为构造
菲波拉契数制 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit St ...
- 11.EL(表达式语言)
一.EL概述 EL(Expression Language,表达式语言)是JSP2.0 中引入的新内容.通过EL可以简化在JSP中对对象的引用,从而规范页面代码,增加程序的可读性和可维护性. 1.EL ...
- Java 读取模板并生成HTML静态文件实例
原理都很简单,主要是对模板的解析.so,我们先准备一个html模板mb.html,做个文件其中的###title###之类的标签用于程序进行查询替换. HTML code复制代码 <html&g ...
- BZOJ2208连通数
还是挺简单的tarjan. 判断时可能重复,直接bitset搞定. 首先tarjan缩点,每个scc的内部肯定能互相到达,更一下,而且一个scc里的各个点的贡献肯定是一样的,topsort,更新答案就 ...
- scrapy pipeline
pipeline的四个方法 @classmethod def from_crawler(cls, crawler): """ 初始化的时候,用以创建pipeline对象 ...
- springboot的@EnableAutoConfiguration起作用的原理
通常我们启动一个springboot项目会在启动方法中增加@SpringBootApplicatoin注解,该注解中包含了@EnableAutoConfiguration @Target(Elemen ...
- TCP之服务与首部
1. TCP 的服务 TCP 通过下列方式提供可靠性: 应用数据被分割成 TCP 认为最适合发送的数据块.与 UDP 不同,UDP 应用程序产生的数据报长度将保持不变.由 TCP 传递给 IP 的信息 ...
- 安装指定版本的Ionic或Cordova
安装ionic 及 cordova npm install -g cordova ionic更新命令 npm update -g cordova ionic安装特定版本 npm install -g ...
- git下载fastadmin
mac git下载后,环境配置运行会如下图 一 下载相对应系统的node.js https://nodejs.org/en/download/ 安装就是一直下一步.... 二 指令终端.cd 到对应项 ...
- 让SpringBoot工程支持热部署
下载地址:https://files.cnblogs.com/files/xiandedanteng/SpringBootWeb-1_20190928.rar 修改Java文件后,每次要重启才好用,修 ...