当前位置:网站首页>AT4108 [ARC094D] Normalization
AT4108 [ARC094D] Normalization
2022-07-30 13:40:00 【With a cool moon】
AT4108 [ARC094D] Normalization
套路了,设 a a a、 b b b、 c c c 分别为 0 0 0、 1 1 1、 2 2 2,Then the sum of the number strings obtained by all operations must be modulo 3 3 3 同余.
And the number string obtained by the operation must have adjacent and equal numbers(Pay attention to whether there are adjacent and equal characters at the beginning of the special judgment).
According to the violent output situation to find when n > 3 n>3 n>3 The above conditions are sufficient conditions.
考虑归纳证明: 如果 n = k n=k n=k time conditions are sufficient,长度为 n = k + 1 n=k+1 n=k+1 的字符串 s s s 和 t t t, s s s It must be possible to change its initials to t t t 的首字母,由于 n = k n=k n=k fully satisfied,即 s s s 和 t t t 后缀 [ 2 , k + 1 ] [2,k+1] [2,k+1] Satisfied enough,Equal initial letters are clearly sufficient at this point,证毕.
故从 s s s (满足 s s s 可操作)variable to t t t,The above two constraints need to be fully satisfied.
故当 n ≤ 3 n\leq 3 n≤3 时 暴力.
Otherwise according to these two restrictions DP
,答案即为满足Digits and Modulo 3 3 3 The number of digit strings with congruent initial strings that contain adjacent equal numbers.
考虑容斥,i.e. digits and modulo 3 3 3 The number of digit strings in the initial congruence string − - − Digits and Modulo 3 3 3 Congruential initial string and 不The number of digit strings containing adjacent equal numbers.
对于前面的,答案显然为 3 n − 1 3^{n-1} 3n−1,i.e. arbitrary enumeration n − 1 n-1 n−1 位,The last one depends on the situation.
对于后面的,DP
Fewer states,记 f i , j , k f_{i,j,k} fi,j,k 表示前 i i i 位,数位和为 j j j,当前位为 k k k The number of strings of digits that satisfy the condition.
转移方程:Enumerates the previous value, 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].
最后,The total answer is 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 Represents the digits and modulo of the initial string 3 3 3 的余数,布尔变量 f l g flg flg Indicates whether the initial string has adjacent equal characters).
时间复杂度 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;
}
边栏推荐
- CMake库搜索函数居然不搜索LD_LIBRARY_PATH
- strlen跟sizeof区别
- jsArray数组复制方法性能测试2207300040
- How to display an Excel table in the body of an email?
- C语言学习练习题:汉诺塔(函数与递归)
- R语言ggpubr包的ggboxplot函数可视化分组箱图、自定义移除可视化图像的特定对象(移除可视化图像轴坐标轴的刻度线标签文本、both x and y axis ticks labels)
- Self-tuning PID self-tuning control 】 【
- Hand tearing read-write lock performance test
- matlab画图,仅显示部分图例
- R语言ggstatsplot包grouped_ggwithinstats函数可视化分组小提琴图、并添加假设检验结果(包含样本数、统计量、效应大小及其置信区间、显著性、组间两两比较、贝叶斯假设)
猜你喜欢
随机推荐
展厅全息投影所具备的三大应用特点
自动化测试的生命周期是什么?
Jackson 的JAR包冲突问题
永州动力电池实验室建设合理布局方案
【Advanced Mathematics】【7】Double Integral
canvas彩虹桥动画js特效
第十三天笔记
每天学一点Scala之 伴生类和伴生对象
TaskDispatcher source code parsing
一本通循环结构的程序设计题解(2)
【软考软件评测师】基于规则说明的测试技术上篇
机器学习——特征选择
大手笔!两所“双一流”大学,获75亿元重点支持!
监控界的最强王者,没有之一!
缓存
Anaconda\Scripts\pip-script.py is not present ? 解决方案
当下,产业园区发展面临的十大问题
缓存一致性
C语言学习练习题:汉诺塔(函数与递归)
Markdown 3 - 流程图表