CF1140E Palindrome-less Arrays
我觉得这道题非常有前途.......
题意:给定一个填了一半的数组,你要把它补完,使之不存在奇回文串,求方案数。字符集为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的更多相关文章
- 【NOIP2017提高A组模拟9.12】Arrays and Palindrome
[NOIP2017提高A组模拟9.12]Arrays and Palindrome[SPJ] 题目 Description Input Output Sample Input 1 6 Sample O ...
- 题解-CF1140E Palindrome-less Arrays
CF1140E Palindrome-less Arrays \(n\) 和 \(k\) 和 \(n\) 个数的序列 \(a\).把 \(a\) 中的 \(-1\) 替换成 \([1,k]\) 之间的 ...
- 【agc001d】Arrays and Palindrome
Portal -->agc001D Description 给你一个\(m\)个数的排列\(A\),这个\(A\)中元素的顺序可以随便调换,\(A\)中的元素的和为\(n\),现在要你构造一个数 ...
- Agc001_D Arrays and Palindrome
传送门 题目大意 给定一个元素和为$N$的有$M$个数的序列$A$,请你可以$A$元素排列的顺序,并需要构造一个有$K$个($K$可以自己定)数的数列,使得任意一个长度为$N$的字符串,若满足:前$A ...
- AGC001 D - Arrays and Palindrome【构造】
把回文串的相等关系连一下,发现最后要求的是一笔画问题 注意到奇数长度的中间有一个单独没有连线的,所以a数组至多有两个奇数值 如果没有奇数,那么b在最前面放一个1,然后把a[1]~a[m-1]放上去,这 ...
- AtCoder Grand Contest 001 D - Arrays and Palindrome
题目传送门:https://agc001.contest.atcoder.jp/tasks/agc001_d 题目大意: 现要求你构造两个序列\(a,b\),满足: \(a\)序列中数字总和为\(N\ ...
- AtCoder AGC001D Arrays and Palindrome (构造)
补一下原来做过的AtCoder思维题的题解 题目链接: https://atcoder.jp/contests/agc001/tasks/agc001_d 先特判一些小的情况. 原题就相当于每个回文串 ...
- Atcoder Grand Contest 001 D - Arrays and Palindrome(构造)
Atcoder 题面传送门 洛谷题面传送门 又是道思维题,又是道把我搞自闭的题. 首先考虑对于固定的 \(a_1,a_2,\dots,a_n;b_1,b_2,\dots,b_m\) 怎样判定是否合法, ...
- 【LeetCode】Palindrome Pairs(336)
1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...
随机推荐
- 创建虚拟目录失败,必须为服务器名称指定“localhost”
关于微信开发过程,远程调试后,再次打开vs出现项目加载失败的解决办法! 第一步: 第二步:打开编辑的页面,把下图这部分直接注释掉 ok了,再加载一次,就好了!
- Java开发之@PostConstruct执行顺序
构造函数==>postConstruct==>init==destory==>predestory==卸载servlet;; 从Java EE5规范开始,Servlet增加了两个影响 ...
- TestNG之测试执行后没有生成默认测试报告(IDEA)
使用IDEA+TestNG进行测试,没有生成 测试报告,是因为没有勾选监听器使用默认报告,具体操作如下: “Run” -> "Edit Configurations" -&g ...
- Reversing-x64Elf-100
一道很简单的小题 作为python小白这道题主要是学习了一点python知识...... 可以看出来 sub_4006FD 这个函数是用来判断输入密码是否正确的 我们看一下它的伪代码: signed ...
- 文件上传.ashx
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Runtime ...
- Linux命令替换字符串
:%s/str1/str2/ 用str2替换str1
- centOS7 下配置和启动maria数据库
从最新版本的linux系统开始,默认的是 Mariadb而不是mysql! 使用系统自带的repos安装很简单: yum install mariadb mariadb-server systemct ...
- Wiener Filter
假设分别有两个WSS process:$x[n]$,$y[n]$,这两个process之间存在某种关系,并且我们也了解这种关系.现在我们手头上有process $x[n]$,目的是要设计一个LTI系统 ...
- Nginx 对上游使用SSL链接
L96 双向认证SSL指令示列 对下游使用证书指令 Syntax: ssl_certificate file; Default: — Context: http, server Syntax: ssl ...
- HTTP协议【详解】——经典面试题
http请求由三部分组成,分别是:请求行.消息报头.请求正文 HTTP(超文本传输协议)是一个基于请求与响应模式的.无状态的.应用层的协议,常基于TCP的连接方式,HTTP1.1版本中给出一种持续连接 ...