我觉得这道题非常有前途.......

题意:给定一个填了一半的数组,你要把它补完,使之不存在奇回文串,求方案数。字符集为k。

n,k<=20w

解:不能有长为三的回文串。也就是不能有两个相隔1的数相同。

发现奇偶下标互相独立,抽出来就是不能有两个相邻的数相同。

我们可以设f[i][0]表示第i位填的跟之后第一个非空位置不同的方案数,f[i][1]是相同。

转移的时候,反正我个大SB分8类分类讨论,还要考虑后面没有非空位置的情况......

 #include <bits/stdc++.h>

 typedef long long LL;
const int N = ;
const LL MO = ; int a[N], n, k, nex[N];
LL f[N][]; int main() {
int cnt = ;
scanf("%d%d", &n, &k); for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
if(i >= && a[i] == a[i - ] && a[i] != -) {
puts("");
return ;
}
} for(int i = n; i >= ; i--) {
if(a[i] != -) nex[i] = a[i];
else nex[i] = nex[i + ];
}
LL ans = ;
f[][] = f[][] = ;
for(int i = ; i <= n; i += ) {
if(a[i] != -) continue;
if(i - < && i + > n) {
ans *= k;
}
else if(i - < && a[i + ] == -) {
f[i][] = nex[i] ? k - : k;
f[i][] = nex[i] ? : ;
}
else if(i - < ) {
ans = ans * (k - ) % MO;
}
else if(i + > n && a[i - ] == -) {
ans = ans * f[i - ][] % MO * (k - ) % MO;
}
else if(i + > n) {
ans = ans * (k - ) % MO;
}
else if(a[i + ] != - && a[i - ] != -) {
ans = ans * (a[i - ] == a[i + ] ? k - : k - ) % MO;
}
else if(a[i - ] != - && a[i + ] == -) {
f[i][] = ((a[i - ] == nex[i] || !nex[i]) ? k - : k - );
f[i][] = ((a[i - ] == nex[i] || !nex[i]) ? : );
}
else if(a[i - ] == - && a[i + ] != -) {
ans = ans * ((f[i - ][] * (k - ) % MO + f[i - ][] * (k - ) % MO) % MO) % MO;
}
else {
f[i][] = (f[i - ][] * (k - ) % MO + f[i - ][] * (nex[i] ? k - : k - ) % MO) % MO;
f[i][] = nex[i] ? f[i - ][] : ;
}
}
LL temp = ans; ans = ;
for(int i = ; i <= n; i += ) {
if(a[i] != -) continue;
if(i - < && i + > n) {
ans *= k;
}
else if(i - < && a[i + ] == -) {
f[i][] = nex[i] ? k - : k;
f[i][] = nex[i] ? : ;
}
else if(i - < ) {
ans = ans * (k - ) % MO;
}
else if(i + > n && a[i - ] == -) {
ans = ans * f[i - ][] % MO * (k - ) % MO;
}
else if(i + > n) {
ans = ans * (k - ) % MO;
}
else if(a[i + ] != - && a[i - ] != -) {
ans = ans * (a[i - ] == a[i + ] ? k - : k - ) % MO;
}
else if(a[i - ] != - && a[i + ] == -) {
f[i][] = ((a[i - ] == nex[i] || !nex[i]) ? k - : k - );
f[i][] = ((a[i - ] == nex[i] || !nex[i]) ? : );
}
else if(a[i - ] == - && a[i + ] != -) {
ans = ans * ((f[i - ][] * (k - ) % MO + f[i - ][] * (k - ) % MO) % MO) % MO;
}
else {
f[i][] = (f[i - ][] * (k - ) % MO + f[i - ][] * (nex[i] ? k - : k - ) % MO) % MO;
f[i][] = nex[i] ? f[i - ][] : ;
}
} printf("%lld\n", ans * temp % MO);
return ;
}

AC代码

CF1140E Palindrome-less Arrays的更多相关文章

  1. 【NOIP2017提高A组模拟9.12】Arrays and Palindrome

    [NOIP2017提高A组模拟9.12]Arrays and Palindrome[SPJ] 题目 Description Input Output Sample Input 1 6 Sample O ...

  2. 题解-CF1140E Palindrome-less Arrays

    CF1140E Palindrome-less Arrays \(n\) 和 \(k\) 和 \(n\) 个数的序列 \(a\).把 \(a\) 中的 \(-1\) 替换成 \([1,k]\) 之间的 ...

  3. 【agc001d】Arrays and Palindrome

    Portal -->agc001D Description 给你一个\(m\)个数的排列\(A\),这个\(A\)中元素的顺序可以随便调换,\(A\)中的元素的和为\(n\),现在要你构造一个数 ...

  4. Agc001_D Arrays and Palindrome

    传送门 题目大意 给定一个元素和为$N$的有$M$个数的序列$A$,请你可以$A$元素排列的顺序,并需要构造一个有$K$个($K$可以自己定)数的数列,使得任意一个长度为$N$的字符串,若满足:前$A ...

  5. AGC001 D - Arrays and Palindrome【构造】

    把回文串的相等关系连一下,发现最后要求的是一笔画问题 注意到奇数长度的中间有一个单独没有连线的,所以a数组至多有两个奇数值 如果没有奇数,那么b在最前面放一个1,然后把a[1]~a[m-1]放上去,这 ...

  6. AtCoder Grand Contest 001 D - Arrays and Palindrome

    题目传送门:https://agc001.contest.atcoder.jp/tasks/agc001_d 题目大意: 现要求你构造两个序列\(a,b\),满足: \(a\)序列中数字总和为\(N\ ...

  7. AtCoder AGC001D Arrays and Palindrome (构造)

    补一下原来做过的AtCoder思维题的题解 题目链接: https://atcoder.jp/contests/agc001/tasks/agc001_d 先特判一些小的情况. 原题就相当于每个回文串 ...

  8. Atcoder Grand Contest 001 D - Arrays and Palindrome(构造)

    Atcoder 题面传送门 洛谷题面传送门 又是道思维题,又是道把我搞自闭的题. 首先考虑对于固定的 \(a_1,a_2,\dots,a_n;b_1,b_2,\dots,b_m\) 怎样判定是否合法, ...

  9. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

随机推荐

  1. innerText兼容问题处理

    IE.Safari.Opera和Chrome支持innerText属性.低版本的火狐浏览器不支持,但支持作用类似的textContent属性.textContent是DOM3级规定的一个属性,而且也得 ...

  2. K3CLOUD常用数据表

    一.数据库查询常用表 --查询数据表select * from ( select convert(varchar(4000),t1.FKERNELXML.query('//TableName')) a ...

  3. Centos6.8 安装nginx

    1.安装相关依赖 (1)yum install gcc 备注:可以通过gcc -v 查看版本信息,来确定是否安装过. (2)yum install pcre-devel (3)yum install ...

  4. windows 10 screenshot keyboard shortcut

    windows 10 screenshot keyboard shortcut Win + Shfit + S https://www.cnet.com/how-to/8-ways-to-take-s ...

  5. Python实现快速排序--数据结构

    快速排序(Quick Sort) 快速排序是由东尼·霍尔所发展的一种排序算法.在平均状况下,排序n个元素要O(nlogn)次比较.在最坏状况下则需要O(n^2)次比较,但这种状况并不常见.事实上,快速 ...

  6. centos安装桌面,下面的几个包缺一不可

    yum groupinstall “X window system” yum groupinstall “Desktop” yum groupinstall “Chinese Support” 不然的 ...

  7. [Codeforces757G]Can Bash Save the Day?——动态点分治(可持久化点分树)

    题目链接: Codeforces757G 题目大意:给出一棵n个点的树及一个1~n的排列pi,边有边权,有q次操作: 1 l r x 求 $\sum\limits_{i=l}^{r}dis(p_{i} ...

  8. hashlib 模块用来进行hash

    hashlib的基本概述: python中的 hashlib 模块用来进行hash 或者md5加密,而且这种加密是不可逆的,所以这种算法又被称为摘要算法, 其支持Opennssl库提供的所有算法,包括 ...

  9. selenium+python启动Firefox浏览器失败问题和点击登陆按钮无效问题

    问题1:使用python+selenium编写脚本调用Firefox时报错:

  10. [USACO18DEC]Balance Beam

    题目链接:这里 或者这里 答案是很显然的,记\(g(i)\)为在\(i\)下平衡木时的期望收益 那么\(g(i)=max(f(i),\frac{g(i-1)+g(i+1)}{2})\) 好了做完了 T ...