Universal Cup Judging System

Universal Cup

حد الوقت: 7.0 s حد الذاكرة: 1024 MB مجموع النقاط: 100 قابلة للهجوم ✓
الإحصائيات

合法表达式的递归定义如下:

  • x 是合法表达式;
  • 如果 $A$ 和 $B$ 是合法表达式,那么 $(AB)$ 也是合法表达式。

例如,((xx)(x(xx))) 是一个包含五个 x 的合法表达式。每个合法表达式都对应一棵二叉树:每个 x 都是一个叶节点,每对括号都对应一个内部节点,其两个子节点是这对括号所包含的两个子表达式。

problem_20730_c95006a543d6017ff34161e94bf0e06a.png

在一次操作中,你可以将形如 $((AB)C)$ 的子表达式替换为 $(A(BC))$,其中 $A$、$B$ 和 $C$ 是任意合法表达式。在对应的树中,这相当于旋转一条边。

problem_20730_a6f26cd498591dfb2673e468ac18b06c.png

如果对表达式 $v$ 执行零次或多次操作可以得到表达式 $u$,我们就称表达式 $u$ 可以由表达式 $v$ 得到。特别地,每个表达式都可以由其自身得到。

给定一个整数 $\ell$。恰好包含 $\ell$ 个 x 的合法表达式的数量是 Catalan 数

$$ n=C_{\ell-1}=\frac{1}{\ell}\binom{2\ell-2}{\ell-1}, $$

所有这些表达式按照字典序依次编号为 $S_1,S_2,\ldots,S_n$,其中字符的顺序为 '(' $<$ 'x' $<$ ')'。这里,$\binom{a}{b}=\frac{a!}{b!(a-b)!}$ 是二项式系数。

还给定一个从 $1$ 到 $n$ 的整数的排列 $p_1,p_2,\ldots,p_n$;值 $p_i$ 表示表达式 $S_{p_i}$。统计满足 $1\le i<j\le n$ 且 $S_{p_i}$ 可以由 $S_{p_j}$ 得到的数对 $(i,j)$ 的数量。

输入格式

第一行包含一个整数 $\ell$($1\le \ell\le 14$)。

第二行包含 $n=C_{\ell-1}$ 个整数 $p_1,p_2,\ldots,p_n$:它们是 $1,2,\ldots,n$ 的一个排列。

输出格式

输出一个整数:满足 $1\le i<j\le n$ 且 $S_{p_i}$ 可以由 $S_{p_j}$ 得到的数对 $(i,j)$ 的数量。

样例

输入格式 1

4
3 1 5 2 4

输出格式 1

3

说明

当 $\ell=4$ 时,有 $n=C_3=5$ 个合法表达式,按字典序依次为:$S_1=$ (((xx)x)x),$S_2=$ ((x(xx))x),$S_3=$ ((xx)(xx)),$S_4=$ (x((xx)x)),$S_5=$ (x(x(xx)))。

一次操作可以将 $S_1$ 变为 $S_2$ 或 $S_3$,将 $S_2$ 变为 $S_4$,以及将 $S_3$ 和 $S_4$ 分别变为 $S_5$;没有任何操作可以应用于 $S_5$。

计入答案的三个数对是:$(i,j)=(1,2)$,因为 $S_3$ 可以由 $S_1$ 得到;$(3,4)$,因为 $S_5$ 可以由 $S_2$ 得到;以及 $(3,5)$,因为 $S_5$ 可以由 $S_4$ 得到。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.