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. AHB SRAM控制器设计

  2. osd磁盘空间足够无法写入数据的分析与解决

    前言 这个问题的来源是ceph社区里面一个群友的环境出现在85%左右的时候,启动osd报错,然后在本地文件系统当中进行touch文件的时候也是报错,df -i查询inode也是没用多少,使用的也是in ...

  3. spring-boot-starter-parent和spring-boot-dependencies

    如何创建一个SpringBoot项目,SpringBoot的依赖引入都是基于starter的,通常创建一个SpringBoot项目都是通过继承关系指定pom文件中的parent. <parent ...

  4. __FUNCTION__

  5. guitar pro系列教程(二十四):Guitar Pro 7 中文界面的介绍

    用过Guitar Pro这款软件的小伙伴们都知道,Guitar Pro这款吉他软件因为是国外开发商研发的,所以软件最初都是英文版本,对于国内的的吉他爱好者来说,在软件使用上还是很不方便的.随着Guit ...

  6. jQuery 第八章 实例方法 遍历索引

    遍历索引相关方法: .each() .index() ------------------------------------------------- .each() 有点像数组的 forEach( ...

  7. 一口气带你读懂80年IT发展史

    计算机的发展历史有多长?真正意义上的计算机诞生,距今也只有80多年的时间.80年,对于每一个人来说,是很长的时间,但对于整个历史来说,只是短短的一瞬间.这八十多年只是整段历史中的一粒尘埃罢了,但却对这 ...

  8. 企业安全06-Apache Log4j Server 反序列化命令执行漏洞(CVE-2017-5645)

    CVE-2017-5645 Apache Log4j Server 反序列化命令执行漏洞(CVE-2017-5645) 一.漏洞原理 Apache Log4j是一个用于Java的日志记录库,其支持启动 ...

  9. Java复数的定义与描述

    1 //4.复数的定义与描述 2 package test; 3 4 import java.util.Scanner; 5 6 public class complex {//复数类 7 doubl ...

  10. presto 访问kudu 多schemas配置

    presto需要访问kudu数据源,但是impala可以直接支持多数据库存储,但是presto不能原生支持,按照presto的官网设置了然而并不起作用. 官方文档: 到官方github提问了,然后并没 ...