ABC134F Permutation Oddness
Problem Statement
Let us define the oddness of a permutation $p = \{p_1, p_2, \dots, p_n\}$ of $\{1, 2, \dots, n\}$ as $\sum_{i=1}^{n} | i - p_i |$. Find the number of permutations of $\{1, 2, \dots, n\}$ of oddness $K$, modulo $10^9+7$.
Constraints
- $1 \le n \le 50$
- $1 \le K \le n^2$
Observation ①
$\sum_{i = 1}^{n} p_i - i = 0$ $\implies$ $\sum_{i = 1}^{n} | p_i - i |$ 是偶数。
进一步有 $\sum_{p_i > i} p_i - i = \sum_{p_i < i} i - p_i = \dfrac{\sum_{i = 1}^{n} | p_i - i |}{ 2}$
Approach
以下内容参考了这篇题解 http://kazune-lab.net/contest/2019/07/20/abc134/ 。
左边的圆形代表数字,右边的方形代表盒子,方形右边的数字是盒子的编号。箭头表示将数字放入盒子中。将 $p_i$ 看成数字 $i$ 所在的盒子的编号。
Observation ②
$|p_i - i|$ = 连接数字 $i$ 与盒子 $p_i$ 的线与 $n - 1$ 条水平虚线 $\ell_1, \ell_2, \dots, \ell_{n-1}$ 的交点数目。
以上图为例,$n = 5$,共有四条水平虚线。
令 $a_j$ 表示连接 $i$ 与 $p_i$ 且 $i < p_i$ 的那些线与第 $j$ 条水平虚线 $\ell_j$ 的交点个数。
以上图为例,满足条件的线有三条(即加粗的那三条线),$a_1 = 1, a_2 = 2, a_3 = 2, a_4 = 1$ 。
Observation ③
$\sum_{i = 1}^{n - 1} a_i = \sum_{i < p_i} p_i - i = \dfrac{\sum_{i = 1}^{n} | p_i - i |}{ 2}$
可以把往盒子里放数字的过程看作下述 $n$ 阶段决策过程。笼统地说,在第 $i$ 个阶段考虑数字 $i$ 和编号为 $i$ 的盒子如何处置。
把在阶段 $i$ 完成之后 $1, 2, \dots, i$ 这些数字中尚未确定要放进哪个盒子的那些数字的集合记作 $S$,初始时 $S$ 为空。
阶段 $i$ 由下述伪代码所描述:
$\mathtt{if}$ 要把 $i$ 放进 $i$ 号盒子
$\qquad$ 把 $i$ 放进 $i$ 号盒子
$\mathtt{elif}$ $S \ne \emptyset$
$\qquad\mathtt{if}$ 要把 $i$ 放到某个编号小于 $i$ 的盒子
$\qquad\qquad$ 确定把 $i$ 放进哪个盒子并把 $i$ 放进去
$\qquad\mathtt{else}$
$\qquad\qquad$ 把 $i$ 加入 $S$
$\qquad\mathtt{if}$ 要把 $S$ 中的某个数放进盒子 $i$
$\qquad\qquad$ 选一个数放进盒子 $i$ 并将其从 $S$ 中删除
$\mathtt{else}$
$\qquad$ 把 $i$ 加入 $S$
以三元组 $(i, j, k)$ 表示第 $i$ 个阶段结束后的状态:
- $j := |S|$(即 $1$ 到 $i$ 这些数中有 $j$ 个要放到编号大于 $i$ 的盒子里)
- $k := \sum_{t = 1}^{i} a_t$ 。
当阶段 $i$ 结束时,$i$ 以后的数字怎么安排尚未考虑,$j$ 个要放在编号大于 $i$ 的盒子里的数字具体怎么放也没确定。
我们感兴趣的最终状态是 $(n, 0, K / 2)$ 。
注:上图中并未画出第 $n$ 条水平虚线 $\ell_n$,因为只有最终状态是 $(n, 0, \cdot)$ 才对应于一个排列,此时必有 $a_n = 0$。
以上图的放置过程为例,状态依次是
$(0, 0, 0) \to (1, 1, 1) \to (2, 2, 3) \to (3, 2, 5) \to (4, 1, 6) \to (5, 0, 6)$
状态转移
$(i, j, k) \to (i + 1, j', k')$
- 把 $i + 1$ 放进 $i + 1$ 号盒子:$(i + 1, j, k + j)$
- $j > 0$,把 $i + 1$ 放进 $x$ 号盒子($x \le i$):$(i + 1, j, k + j)$
- $j > 0$,把 $S$ 中的某个数放进 $i$ 号盒子:$(i + 1, j, k + j)$
- $j > 0$,把 $i + 1$ 放进 $x$ 号盒子($x \le i$)并把 $S$ 中的某个数放进 $i$ 号盒子:$(i + 1, j - 1, k + j - 1)$
- $S$ 中的数以及 $i+1$ 所在的盒子编号都大于 $i + 1$:$(i + 1, j + 1, k + j + 1)$
References
ABC134F Permutation Oddness的更多相关文章
- AtCoder刷题记录
构造题都是神仙题 /kk ARC066C Addition and Subtraction Hard 首先要发现两个性质: 加号右边不会有括号:显然,有括号也可以被删去,答案不变. \(op_i\)和 ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- Permutation test: p, CI, CI of P 置换检验相关统计量的计算
For research purpose, I've read a lot materials on permutation test issue. Here is a summary. Should ...
随机推荐
- 项目部署中,tomcat报java.lang.OutOfMemoryError: PermGen space
原因: PermGen space的全称是Permanent Generation space,是指内存的永久保存区域,这块内存主要是被JVM存放Class和Meta信息的,Class在被Loader ...
- vue中改变数组或对象,页面没做出对应的渲染
原文链接 数组更新检测 变异方法 Vue 包含一组观察数组的变异方法,所以它们也将会触发视图更新.这些方法如下: push() pop() shift() unshift() splice() sor ...
- jquery文章链接
好文链接 1.jQuery是js的一个库,封装了js中常用的逻辑: 2.调用jQuery: (1).本地调用,在script标签的src属性里写上jQuery文件的地址. (2).使用CDN调用jQu ...
- Oracle实现分页,每页有多少条记录数
分页一直都是关系数据库的热门,在数据量非常多的情况下,需要根据分页展示,每页展示多少条记录,以此减轻数据的压力; 1实现原理,根据rownum取记录数,根据公式(页数-1)*每页想要展示的记录数 AN ...
- python-matplotlib-2
figure的使用 x = np.linspace(-1, 1, 50) y1 = 2 * x + 1 #figure 1 plt.figure() plt.plot(x, y1) # figure ...
- mysql字符串函数:FIND_IN_SET()使用方法详解
语法: FIND_IN_SET(str,strlist) 第一个参数str是要查找的字符串. 第二个参数strlist是要搜索的逗号分隔的字符串列表. 假如字符串str 在由N 子链组成的字符串列表s ...
- React 番外篇
小技巧:如果我们想了解一门技术,不知道如何学习,那就在 BOSS 直聘上,来看看对这门技术的要求 这篇给大家讲的是 React 1.0 的初始版本,仅仅是让大家有个了解,毕竟回顾历史,我们才能找到他最 ...
- yum 时一直停在Determining fastest mirrors 界面
[root@fanyk ~]# yum redis Loaded plugins: fastestmirror Determining fastest mirrors 在yum makecache时, ...
- mybatis的mapper映射文件
1概述1.1应用架构 mybatis框架用于支持对关系数据库的操作,该体系的应用架构如下图所示: 在mybatis框架体系中,主要的组件是:SqlSessionFactoryBean和Mapp ...
- GDAL OGR Tools
OGR2OGR ogr2ogr 使用文档 1. GeoJSON 转换 ShapeFile ogr2ogr -nlt POLYGON -skipfailures ground.shp ground.js ...