当前位置:网站首页>AT4108 [ARC094D] Normalization
AT4108 [ARC094D] Normalization
2022-07-30 13:17:00 【心怀凉月】
AT4108 [ARC094D] Normalization
套路了,设 a a a、 b b b、 c c c 分别为 0 0 0、 1 1 1、 2 2 2,则所有操作得到的数字串的和必然模 3 3 3 同余。
且操作得到的数字串必然有相邻相等的数字(注意特判开始是否有相邻相等的字符)。
根据暴力输出情况发现当 n > 3 n>3 n>3 时上述条件为充分条件。
考虑归纳证明: 如果 n = k n=k n=k 时条件充分,长度为 n = k + 1 n=k+1 n=k+1 的字符串 s s s 和 t t t, s s s 一定能够经过若干次操作将其首字母换成 t t t 的首字母,由于 n = k n=k n=k 时满足充分,即 s s s 和 t t t 后缀 [ 2 , k + 1 ] [2,k+1] [2,k+1] 满足充分,此时首字母相等显然也充分,证毕。
故从 s s s (满足 s s s 可操作)可变到 t t t,需充分满足上面两个限制。
故当 n ≤ 3 n\leq 3 n≤3 时 暴力。
否则根据这两个限制 DP,答案即为满足数位和模 3 3 3 同余初始串且含有相邻相等的数字的数字串个数。
考虑容斥,即数位和模 3 3 3 同余初始串的数字串个数 − - − 数位和模 3 3 3 同余初始串且不含有相邻相等的数字的数字串个数。
对于前面的,答案显然为 3 n − 1 3^{n-1} 3n−1,即任意枚举 n − 1 n-1 n−1 位,最后一位视情况而定。
对于后面的,DP 状态更少,记 f i , j , k f_{i,j,k} fi,j,k 表示前 i i i 位,数位和为 j j j,当前位为 k k k 的满足条件的数字串个数。
转移方程:枚举上一位取值, f i , j , k = ∑ l = 0 2 f i − 1 , ( j − k ) m o d 3 , l [ l ≠ k ] f_{i,j,k}=\sum\limits_{l=0}^{2}f_{i-1,(j-k)\bmod 3,l}[l \ne k] fi,j,k=l=0∑2fi−1,(j−k)mod3,l[l=k]。
最后,总答案即为 3 n − 1 − ∑ i = 0 2 f n , s , i − f l g 3^{n-1}-\sum\limits_{i=0}^{2}f_{n,s,i}-flg 3n−1−i=0∑2fn,s,i−flg(其中 s s s 表示初始字符串的数位和模 3 3 3 的余数,布尔变量 f l g flg flg 表示初始字符串是否有相邻相等的字符)。
时间复杂度 O ( n ) \mathcal O(n) O(n)。
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
#define ha putchar(' ')
#define he putchar('\n')
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return x * f;
}
inline void write(int x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9)
write(x / 10);
putchar(x % 10 + 48);
}
const int _ = 2e5 + 10, mod = 998244353;
int n, S, f[_][3][3], ans;
string s;
int qpow(int x, int y) {
int res = 1;
while (y) {
if (y & 1)res = res * x % mod;
x = x * x % mod;
y >>= 1;
}
return res;
}
queue<string> q;
map<string, bool> mp;
char js(int x)
{
x %= 3;
if(!x) return 'a';
if(x == 1) return 'c';
return 'b';
}
void solve()
{
q.push(s);
mp[s] = 1;
while(!q.empty())
{
string nw = q.front();
q.pop();
// cout << nw << "\n";
for(int i = 0; i < n - 1; ++i)
if(nw[i] != nw[i + 1])
{
string ss = nw;
ss[i] = ss[i + 1] = js(nw[i] - 'a' + nw[i + 1] - 'a');
if(mp.find(ss) == mp.end())
{
q.push(ss);
mp[ss] = 1;
}
}
}
// for(auto v : mp) cout << v.first << "\n";
write(mp.size()), he;
}
signed main() {
cin >> s;
n = s.size();
bool flg = 0;
for (int i = 0; i < n - 1; ++i)
if (s[i] != s[i + 1]) {
flg = 1;
break;
}
if (!flg) return puts("1"), 0;
if(n <= 3) return solve(), 0;
flg = 1;
for (int i = 0; i < n - 1; ++i)
if (s[i] == s[i + 1]) {
flg = 0;
break;
}
for (int i = 0; i < n; ++i) S = (S + s[i] - 'a') % 3;
ans = qpow(3, n - 1);
for (int i = 0; i <= 2; ++i) f[0][i][i] = 1;
for (int i = 1; i < n; ++i)
for (int j = 0; j <= 2; ++j)
for (int k = 0; k <= 2; ++k)
for (int l = 0; l <= 2; ++l)
if (l != k) f[i][j][k] = (f[i][j][k] + f[i - 1][(j - k + 3) % 3][l]) % mod;
for (int i = 0; i <= 2; ++i) ans = (ans - f[n - 1][S][i] + mod) % mod;
write(ans + flg), he;
return 0;
}
边栏推荐
猜你喜欢

Heshu Group: Make smart cities smarter and make real life better

一本通循环结构的程序设计第一章题解(1)

【河北工业大学】考研初试复试资料分享

程序员修炼之道:务以己任,实则明心——通向务实的最高境界

cpu/CS and IP

【语音识别】基于GMM-HMM的语音识别系统
SQL 26 calculation under 25 years of age or older and the number of users

一本通循环结构的程序设计题解(2)

重保特辑|筑牢第一道防线,云防火墙攻防演练最佳实践

leetcode207.课程表(判断有向图是否有环)
随机推荐
湖仓一体电商项目(二):项目使用技术及版本和基础环境准备
电流电压采集模块DAM-6160
【23考研】408代码题参考模板——链表
Dolphinscheduler stand-alone transformation
CV-Model【2】:MobileNet v1
自从外包干了四年,基本废了...
libudev manual
What are the hard-core upgrades and applications that cannot be missed in Greenplum 6.0?
手撕读写锁性能测试
Yilian: Activating the Value Potential of Data Elements and Unleashing the Innovation Dividend of SAS SSD
【语音识别】基于GMM-HMM的语音识别系统
R语言ggplot2可视化:使用ggpubr包的ggboxplot函数可视化分组箱图、使用ggpar函数改变图形化参数(ylim、修改可视化图像y轴坐标轴数值范围)
EasyNVR更新版本至(V5.3.0)后页面不显示通道配置该如何解决?
SQL 26 calculation under 25 years of age or older and the number of users
dolphinscheduler添加hana支持
[PostgreSQL] - 存储结构及缓存shared_buffers
腰部外骨骼机器人线性自抗扰控制器参数优化
剑指 Offer 05. 替换空格
434. 字符串中的单词数
【高等数学】【7】二重积分