题解-CF1140E Palindrome-less Arrays
\(n\) 和 \(k\) 和 \(n\) 个数的序列 \(a\)。把 \(a\) 中的 \(-1\) 替换成 \([1,k]\) 之间的整数。求使 \(a\) 中不存在长度为奇数的回文串的方案数。
数据范围:\(2\le n,k\le 2\cdot 10^5\),\(a_i=-1{\rm~or~}a_i\in[1,k]\)。
题目限制即不能有 \(a_i=a_{i-2}\)。
令 \(b_i=a_{2i},c_i=a_{2i-1}\)。
答案为序列 \(b\) 和 \(c\) 填成相邻两数不等的方案数积。
如填 \(x,-1,-1,...,-1,y\) 这段 \(i\) 个 \(-1\) 使相邻两数不等:
令 \(f_i\) 表示 \(x=y\) 的填法方案数,\(g_i\) 表示 \(x\not=y\) 的填法方案数。
\begin{cases}
0&(i=0)\\
g_{i-1}(k-1)&{\rm else}\\
\end{cases}\\\\
g_i=
\begin{cases}
1&(i=0)\\
g_{i-1}(k-2)+f_{i-1}&{\rm else}\\
\end{cases}
\]
最后把每段 \(-1\) 的答案乘起来就是填 \(b,c\) 的方案数。
边界的处理具体看代码。
- 代码
#include <bits/stdc++.h>
using namespace std;
//Start
typedef long long ll;
typedef double db;
#define mp(a,b) make_pair(a,b)
#define x first
#define y second
#define b(a) a.begin()
#define e(a) a.end()
#define sz(a) int((a).size())
#define pb(a) push_back(a)
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
//Data
const int N=2e5;
const int mod=998244353;
int n,k,a[N+7];
//DP
int f[2][(N<<1)+7];
int bb,b[(N>>1)+7],cc,c[(N>>1)+7];
void Pre(){
f[0][0]=0,f[1][0]=1;
for(int i=1;i<=(n>>1)+1;i++){
f[0][i]=(ll)f[1][i-1]*(k-1)%mod;
f[1][i]=((ll)f[1][i-1]*(k-2)%mod+f[0][i-1])%mod;
}
}
int DP(int cnt,int s[]){
int len=0,lst=0,res=1;
for(int i=1;i<=cnt;i++){
if(s[i]==-1) len++;
else {
if(len){
if(!lst) res=(ll(k-1)*f[1][len-1]+f[0][len-1])%mod*res%mod;
else if(lst==s[i]) res=(ll)f[0][len]*res%mod;
else res=(ll)f[1][len]*res%mod;
}
len=0,lst=s[i];
}
}
if(len){
if(!lst){
if(len==1) res=k;
else res=(ll(k-1)*f[1][len-2]+f[0][len-2])*k%mod*res%mod;
} else res=(ll(k-1)*f[1][len-1]+f[0][len-1])%mod*res%mod;
}
return res;
}
//Main
int main(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
if(~a[i]&&i-2>=1&&a[i]==a[i-2]) return puts("0"),0;
if(i&1) c[++cc]=a[i];
else b[++bb]=a[i];
}
Pre();
printf("%d\n",(ll)DP(cc,c)*DP(bb,b)%mod);
return 0;
}
祝大家学习愉快!
题解-CF1140E Palindrome-less Arrays的更多相关文章
- 【题解】Palindrome pairs [Codeforces159D]
[题解]Palindrome pairs [Codeforces159D] 传送门:\(Palindrome\) \(pairs\) \([CF159D]\) [题目描述] 给定一个长度为 \(N\) ...
- [LeetCode 题解]:Palindrome Number
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Determine ...
- leetcode题解 9. Palindrome Number
9. Palindrome Number 题目: Determine whether an integer is a palindrome. Do this without extra space. ...
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...
- [LeetCode 题解]: Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode(125)题解--Valid Palindrome
https://leetcode.com/problems/valid-palindrome/ 题目: Given a string, determine if it is a palindrome, ...
- 算法与数据结构基础 - 哈希表(Hash Table)
Hash Table基础 哈希表(Hash Table)是常用的数据结构,其运用哈希函数(hash function)实现映射,内部使用开放定址.拉链法等方式解决哈希冲突,使得读写时间复杂度平均为O( ...
- 算法与数据结构基础 - 双指针(Two Pointers)
双指针基础 双指针(Two Pointers)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...
- LeetCode in action
(1) Linked List: 2-add-two-numbers,2.cpp 19-remove-nth-node-from-end-of-list,TBD 21-merge-two-sorted ...
随机推荐
- 【干货!!】十分钟带你搞懂 Java AQS 核心设计与实现!!!
前言 这篇文章写完放着也蛮久的了,今天终于发布了,对于拖延症患者来说也真是不容易-哈哈哈. 言归正传,其实吧..我觉得对于大部分想了解 AQS 的朋友来说,明白 AQS 是个啥玩意儿以及为啥需要 AQ ...
- 运维自动化之11 - 自动化部署之jenkins及简介
https://www.cnblogs.com/jimmy-xuli/p/9020825.html
- 如何用思维导图软件MindManager制作项目管理图表
项目管理的官方解释为:运用各种相关技能.方法与工具,为满足或超越项目有关各方对项目的要求与期望,所开展的各种计划.组织.领导.控制等方面的活动. 其实使用MindManager思维导图软件来创建项目管 ...
- mysql 分组查询
mysql 分组查询 获取id最大的一条 (1)分组查询获取最大id SELECT MAX(id) as maxId FROM `d_table` GROUP BY `parent_id` ; (2) ...
- Python正则表达式大全
前言 正则表达式是对字符串(包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为"元字符"))操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成 ...
- Java基础教程——命令行运行Java代码
视屏讲解:https://www.bilibili.com/video/av48196406/?p=4 命令行运行Java代码 (1)使用记事本新建文本文件[Test.java]. 注意,默认状态下W ...
- Mybatis【2.2】-- Mybatis关于创建SqlSession源码分析的几点疑问?
代码直接放在Github仓库[https://github.com/Damaer/Mybatis-Learning ],可直接运行,就不占篇幅了. 目录 1.为什么我们使用SQLSessionFact ...
- sentinel整合dubbo
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-dubbo-a ...
- SpringBoot 实现微信推送模板
导读 由于最近手头上需要做个Message Gateway,涉及到:邮件(点我直达).短信.公众号等推送功能,网上学习下,整理下来以备以后使用. 添加依赖 在SpringBoot项目中添加依赖 < ...
- PADS经验总结
PADS经验总结 1. 快捷键z+数字,能够快速查看相应层:直接z,会显示所有层: 2. 快捷键l+数字,在走线时能够快速切换层: 3. setup->design Rules能设置线宽,DRC ...