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 ...
随机推荐
- Qbxt 模拟题 day3(am) T3 选数字 (select)(贪心)
选数字 (select Time Limit:3000ms Memory Limit:64MB 题目描述 LYK 找到了一个 n*m 的矩阵,这个矩阵上都填有一些数字,对于第 i 行第 j 列的位置上 ...
- 笔记本在安装Windows+Linux双系统后,进入Windows时花屏的解决办法
问题:在笔记本安装双系统(Windows7+Ubuntu14.04)[先安装Windows,后安装Ubuntu]后,进入Windows时出现了花屏. 问题原因:笔记本只有集显,在系统启动时会先加载Ub ...
- BeautifulSoup4 提取数据爬虫用法详解
Beautiful Soup 是一个HTML/XML 的解析器,主要用于解析和提取 HTML/XML 数据. 它基于 HTML DOM 的,会载入整个文档,解析整个 DOM树,因此时间和内存开销都会大 ...
- TCP输出 之 tcp_write_xmit
概述 tcp_write_xmit函数完成对待发送数据的分段发送,过程中会遍历发送队列,进行窗口检查,需要TSO分段则分段,然后调用tcp_transmit_skb发送数据段: 源码分析 static ...
- TCP主动打开 之 第一次握手-发送SYN
tcp客户端与服务器端建立连接需要经过三次握手过程,本文主要分析客户端主动打开中的第一次握手部分,即客户端发送syn段到服务器端: tcp_v4_connect为发起连接主流程,首先对必要参数进行检查 ...
- python3笔记二十四:Mysql数据库操作命令
一:学习内容 Mysql操作命令:启动服务.停止服务.连接数据库.退出数据库.查看版本.显示当前时间.远程连接 数据库操作命令:创建数据库.删除数据库.切换数据库.查看当前选择的数据库 表操作命令:查 ...
- airflow当触发具有多层subDAG的任务的时候,出现[Duplicate entry ‘xxxx’ for key dag_id]的错误的问题处理
当触发一个具有多层subDAG的任务时,会发现执行触发的task任务运行失败,但是需要触发的目标DAG已经在运行了,dag log 错误内容: [2019-11-21 17:47:56,825] {b ...
- ubuntu下如何高速下载?
答: 使用uget工具 1.安装uget sudo apt-get install uget -y 2.下载时在设置里指定最大连接数 笔者指定最大连接数为10,可以适当调整此值
- LC 763. Partition Labels
A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...
- Numpy中matrix()和array()的区别
matrix() 和 array() 的区别,主要从以下方面说起: 1. 矩阵生成方式不同 import numpy as np a1 = np.array([[1, 2], [3, 4]]) b1 ...