CF1768E.Partial Sorting

提高+/省选-

通过率:0%

时间限制:1.50s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Consider a permutation†^\dagger pp of length 3n3n. Each time you can do one of the following operations:

  • Sort the first 2n2n elements in increasing order.
  • Sort the last 2n2n elements in increasing order.

We can show that every permutation can be made sorted in increasing order using only these operations. Let's call f(p)f(p) the minimum number of these operations needed to make the permutation pp sorted in increasing order.

Given nn, find the sum of f(p)f(p) over all (3n)!(3n)! permutations pp of size 3n3n.

Since the answer could be very large, output it modulo a prime MM.

†^\dagger A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

考虑一个长度为 3n3n 的排列†^\dagger pp。每次你可以执行以下两种操作之一:

  • 将前 2n2n 个元素按升序排序;
  • 将后 2n2n 个元素按升序排序。

可以证明:仅使用这两种操作,任意排列 pp 均可被变为升序排列。记 f(p)f(p) 为将排列 pp 变为升序排列所需的最少操作次数。

给定 nn,求所有 (3n)!(3n)! 个长度为 3n3n 的排列 pp 对应的 f(p)f(p) 之和。

由于答案可能非常大,请对一个给定质数 MM 取模后输出。

†^\dagger 长度为 nn 的排列是指由 11 到 nn 中互不相同的 nn 个整数以任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是排列(数字 22 在数组中出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中出现了 44)。

输入格式

The only line of input contains two numbers nn and MM (1≤n≤1061 \leq n \leq 10^6, 108≤M≤10910^8 \leq M \leq 10^9). It is guaranteed that MM is a prime number.

输入仅包含一行,其中有两个数 nn 和 MM(1≤n≤1061 \leq n \leq 10^6,108≤M≤10910^8 \leq M \leq 10^9)。保证 MM 是一个质数。

输出格式

Output the answer modulo MM.

输出答案对 MM 取模的结果。

输入输出样例

  • 输入#1

    1 100009067

    输出#1

    9
  • 输入#2

    2 100000357

    输出#2

    1689
  • 输入#3

    69 999900997

    输出#3

    193862705

说明/提示

In the first test case, all the permutations are:

  • [1,2,3][1, 2, 3], which requires 00 operations;
  • [1,3,2][1, 3, 2], which requires 11 operation;
  • [2,1,3][2, 1, 3], which requires 11 operation;
  • [2,3,1][2, 3, 1], which requires 22 operations;
  • [3,1,2][3, 1, 2], which requires 22 operations;
  • [3,2,1][3, 2, 1], which requires 33 operations.

Therefore, the answer is 0+1+1+2+2+3=90+1+1+2+2+3=9.

在第一个测试用例中,所有排列如下:

  • [1,2,3][1, 2, 3],需要 00 次操作;
  • [1,3,2][1, 3, 2],需要 11 次操作;
  • [2,1,3][2, 1, 3],需要 11 次操作;
  • [2,3,1][2, 3, 1],需要 22 次操作;
  • [3,1,2][3, 1, 2],需要 22 次操作;
  • [3,2,1][3, 2, 1],需要 33 次操作。

因此,答案为 0+1+1+2+2+3=90+1+1+2+2+3=9。

输入解题思路,AI测评打分。不知道怎么写?

首页