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\) 的填法方案数。

\[f_i=
\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的更多相关文章

  1. 【题解】Palindrome pairs [Codeforces159D]

    [题解]Palindrome pairs [Codeforces159D] 传送门:\(Palindrome\) \(pairs\) \([CF159D]\) [题目描述] 给定一个长度为 \(N\) ...

  2. [LeetCode 题解]:Palindrome Number

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Determine ...

  3. leetcode题解 9. Palindrome Number

    9. Palindrome Number 题目: Determine whether an integer is a palindrome. Do this without extra space. ...

  4. 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...

  5. [LeetCode 题解]: Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  6. LeetCode(125)题解--Valid Palindrome

    https://leetcode.com/problems/valid-palindrome/ 题目: Given a string, determine if it is a palindrome, ...

  7. 算法与数据结构基础 - 哈希表(Hash Table)

    Hash Table基础 哈希表(Hash Table)是常用的数据结构,其运用哈希函数(hash function)实现映射,内部使用开放定址.拉链法等方式解决哈希冲突,使得读写时间复杂度平均为O( ...

  8. 算法与数据结构基础 - 双指针(Two Pointers)

    双指针基础 双指针(Two Pointers)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...

  9. 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 ...

随机推荐

  1. linux之DNS服务

    1.DNS (Domain Name Service 域名解析) DNS是因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网而不需要记忆能够直接被机器识别的IP. BI ...

  2. MFC详解

    MFC的消息响应机制详解: 1.MFC是Windows下程序设计的最流行的一个类库,但是该类库比较庞杂,尤其是它的消息映射机制,更是涉及到很多低层的东西,接下来详细讲解. 2.在讲解MFC的消息响应之 ...

  3. pytorch框架对RTX 2080Ti RTX 3090的支持与性能测试

    时间点:202011-18 一.背景 2020年9月nvidia发布了30系列的显卡.比起20系列网上的评价是:性能翻倍,价格减半. 最近正好本人手上有RTX 2080Ti 和 RTX 3090,所以 ...

  4. springboot做邮件发送功能时报错No qualifying bean of type 'org.springframework.mail.javamail.JavaMailSender' available:的问题解决方案

    1.检查application.yml中的配置是否正确 spring.mail.host=smtp.xxx.comspring.mail.username=xxx@xxx.comspring.mail ...

  5. flink1.10版local模式提交job流程分析

    1.WordCount程序实例 2.本地监听9000端口后测试结果 3.job提交流程 4.local模式执行StreamGraph任务 5.流程分析 flink job提交流程个人理解可以大致分为定 ...

  6. spring cloud 入门系列

    springcloud入门总结转发自:https://www.cnblogs.com/sam-uncle/p/9340390.html 最近看到微服务很火,也是未来的趋势, 所以就去学习下,在dubb ...

  7. Python_网络编程_socket()

    什么是 Socket? Socket又称"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求,使主机间或者一台计算机上的进程间可以通讯. 详细资 ...

  8. 最大子段和问题(C/C++)

    Description 给定有n个整数(可能为负整数)组成的序列a1,a2,...,an,求该序列连续的子段和的最大值. 如果该子段的所有元素和是负整数时定义其最大子段和为0. Input 第一行有一 ...

  9. php(tp5)生成条形码

    因为公司业务需要,研究了一下条形码 1.下载barcodegen扩展包 官网地址:https://www.barcodebakery.com 2.下载完后解压至 extend 文件夹里面,然后复制以下 ...

  10. phpstorm换行符设置LF

    git执行命令行 git config --global core.autocrlf true