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

题意:给定一个填了一半的数组,你要把它补完,使之不存在奇回文串,求方案数。字符集为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. 常用Mac快捷键

    1.复制Cmd + C 粘贴Cmd + C —-> Cmd + V 剪切Cmd + C —-> Cmd + Opt + V 2.查看隐藏文件 Cmd + shift + . 3. 货币符号 ...

  2. onbeforeunload事件两种写法及效果

    在符合W3C标准的浏览器里,可以使用addEventListener方法来添加事件. 当不需要为一个事件添加多个处理函数的时候,可以简单的使用onXXX=function(){}的方式来添加事件处理函 ...

  3. EChart.js 笔记二

    交互组件 Echart.js 中交互组件比较多.例如: legend(图例).title(标题组件).visualMap(视觉映射组件).dataZoom(数据缩放组件).timeline(时间线组件 ...

  4. 【python练习题】程序9

    #题目:暂停一秒输出. import time for i in range(5): print (i) time.sleep(1)

  5. 转载:实现MATLAB2016a和M文件关联

    转载自http://blog.csdn.net/qq_22186119 新安装MATLAB2016a之后,发现MATLAB没有和m文件关联 每次打开m文件后都会重新打开一次MATLAB主程序 后来发现 ...

  6. HDU 1074 Doing Homework(经典状压dp)

    题目链接  Doing Homework        Ignatius has just come back school from the 30th ACM/ICPC. Now he has a ...

  7. Qt setStyleSheet

    Qt中设置按钮或QWidget的外观是,可以使用QT Style Sheets来进行设置,非常方便.可以用setStyleSheet("font: bold; font-size:20px; ...

  8. MD5进行解密操作

    package com.dyy.test; import java.security.MessageDigest; public class TestMD5Util { /*** * MD5加码 生成 ...

  9. Codeforces519 E. A and B and Lecture Rooms

    传送门:>Here< 题意:询问给出一棵无根树上任意两点$a,b$,求关于所有点$i$,$dist(a,i) = dist(b,i)$的点的数量.要求每一次询问在$O(log n)$的时间 ...

  10. 用递归方法判断字符串是否是回文(Recursion Palindrome Python)

    所谓回文字符串,就是一个字符串从左到右读和从右到左读是完全一样的.比如:"level" .“aaabbaaa”. "madam"."radar&quo ...