当前位置:网站首页>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;
}
边栏推荐
- qq udp tcp机制
- TaskDispatcher源码解析
- js男女身高体重关系图
- strlen跟sizeof区别
- Anaconda\Scripts\pip-script.py is not present ? 解决方案
- CMake库搜索函数居然不搜索LD_LIBRARY_PATH
- 判断链表是否有环
- How to migrate the device data connected by RTSP of EasyCVR platform to EasyNVR?
- 腾讯称电竞人才缺口200万;华为鸿蒙3.0正式发布;乐视推行每周工作4天半?...丨黑马头条...
- What are the hard-core upgrades and applications that cannot be missed in Greenplum 6.0?
猜你喜欢
随机推荐
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
R语言ggplot2可视化:使用ggpubr包的ggmaplot函数可视化MA图(MA-plot)、设置label.select参数自定义在图中显示标签的基因类型(自定义显示的标签列表)
干货分享:小技巧大用处之Bean管理类工厂多种实现方式
[Go]四、模块和包、流程控制、结构体
qq udp tcp机制
R语言向前或者向后移动时间序列数据(自定义滞后或者超前的期数):使用dplyr包中的lag函数将时间序列数据向后移动一天(设置参数n为负值)
What are the hard-core upgrades and applications that cannot be missed in Greenplum 6.0?
05 | 后台登录:基于账号密码的登录方式(下)
阿里 P7 到底是怎样的水平?
Dolphinscheduler stand-alone transformation
MQTT网关读取西门子PLC数据传输到阿里云平台案例教程
datax enables hana support and dolphinscheduler enables datax tasks
Self-tuning PID self-tuning control 】 【
Hu-cang integrated e-commerce project (1): project background and structure introduction
20220729 证券、金融
程序员修炼之道:务以己任,实则明心——通向务实的最高境界
jsArray array copy method performance test 2207292307
缓存
libudev 使用说明书
Logic Vulnerability----Permission Vulnerability









