CF1284C.New Year and Permutation

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

Recall that the permutation 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).

A sequence aa is a subsegment of a sequence bb if aa can be obtained from bb by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l,r][l, r], where l,rl, r are two integers with 1≤l≤r≤n1 \le l \le r \le n. This indicates the subsegment where l−1l-1 elements from the beginning and n−rn-r elements from the end are deleted from the sequence.

For a permutation p1,p2,…,pnp_1, p_2, \ldots, p_n, we define a framed segment as a subsegment [l,r][l,r] where max⁡pl,pl+1,…,pr−min⁡pl,pl+1,…,pr=r−l\max{p_l, p_{l+1}, \dots, p_r} - \min{p_l, p_{l+1}, \dots, p_r} = r - l. For example, for the permutation (6,7,1,8,5,3,2,4)(6, 7, 1, 8, 5, 3, 2, 4) some of its framed segments are: [1,2],[5,8],[6,7],[3,3],[8,8][1, 2], [5, 8], [6, 7], [3, 3], [8, 8]. In particular, a subsegment [i,i][i,i] is always a framed segments for any ii between 11 and nn, inclusive.

We define the happiness of a permutation pp as the number of pairs (l,r)(l, r) such that 1≤l≤r≤n1 \le l \le r \le n, and [l,r][l, r] is a framed segment. For example, the permutation [3,1,2][3, 1, 2] has happiness 55: all segments except [1,2][1, 2] are framed segments.

Given integers nn and mm, Jongwon wants to compute the sum of happiness for all permutations of length nn, modulo the prime number mm. Note that there exist n!n! (factorial of nn) different permutations of length 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)。

若序列 aa 可通过从序列 bb 的开头删除若干(可能为零个或全部)元素、并从结尾删除若干(可能为零个或全部)元素而得到,则称 aa 是 bb 的一个子段。我们将子段记作 [l,r][l, r],其中 l,rl, r 是满足 1≤l≤r≤n1 \le l \le r \le n 的两个整数,表示从原序列中删去开头的 l−1l-1 个元素和结尾的 n−rn-r 个元素后所剩下的部分。

对于一个排列 p1,p2,…,pnp_1, p_2, \ldots, p_n,我们定义一个带框子段(framed segment) 为满足如下条件的子段 [l,r][l,r]:

max⁡{pl,pl+1,…,pr}−min⁡{pl,pl+1,…,pr}=r−l.\max\{p_l, p_{l+1}, \dots, p_r\} - \min\{p_l, p_{l+1}, \dots, p_r\} = r - l.

例如,对排列 (6,7,1,8,5,3,2,4)(6, 7, 1, 8, 5, 3, 2, 4),其部分带框子段包括:[1,2],[5,8],[6,7],[3,3],[8,8][1, 2], [5, 8], [6, 7], [3, 3], [8, 8]。特别地,对任意 ii(1≤i≤n1 \le i \le n),单元素子段 [i,i][i,i] 总是一个带框子段。

我们定义一个排列 pp 的幸福值(happiness) 为满足 1≤l≤r≤n1 \le l \le r \le n 且 [l,r][l, r] 是带框子段的数对 (l,r)(l, r) 的个数。例如,排列 [3,1,2][3, 1, 2] 的幸福值为 55:除子段 [1,2][1, 2] 外,其余所有子段均为带框子段。

给定整数 nn 和 mm,Jongwon 希望计算所有长度为 nn 的排列的幸福值之和,并对素数 mm 取模。注意:长度为 nn 的不同排列共有 n!n!(nn 的阶乘)个。

输入格式

The only line contains two integers nn and mm (1≤n≤250 0001 \le n \le 250\,000, 108≤m≤10910^8 \le m \le 10^9, mm is prime).

唯一一行包含两个整数 nn 和 mm(1≤n≤250 0001 \le n \le 250\,000,108≤m≤10910^8 \le m \le 10^9,且 mm 为质数)。

输出格式

Print rr (0≤r<m0 \le r \lt m), the sum of happiness for all permutations of length nn, modulo a prime number mm.

输出 rr(0≤r<m0 \le r \lt m),即所有长度为 nn 的排列的幸福值之和对素数 mm 取模的结果。

输入输出样例

  • 输入#1

    1 993244853

    输出#1

    1
  • 输入#2

    2 993244853

    输出#2

    6
  • 输入#3

    3 993244853

    输出#3

    32
  • 输入#4

    2019 993244853

    输出#4

    923958830
  • 输入#5

    2020 437122297

    输出#5

    265955509

说明/提示

For sample input n=3n=3, let's consider all permutations of length 33:

  • [1,2,3][1, 2, 3], all subsegments are framed segment. Happiness is 66.
  • [1,3,2][1, 3, 2], all subsegments except [1,2][1, 2] are framed segment. Happiness is 55.
  • [2,1,3][2, 1, 3], all subsegments except [2,3][2, 3] are framed segment. Happiness is 55.
  • [2,3,1][2, 3, 1], all subsegments except [2,3][2, 3] are framed segment. Happiness is 55.
  • [3,1,2][3, 1, 2], all subsegments except [1,2][1, 2] are framed segment. Happiness is 55.
  • [3,2,1][3, 2, 1], all subsegments are framed segment. Happiness is 66.

Thus, the sum of happiness is 6+5+5+5+5+6=326+5+5+5+5+6 = 32.

对于样例输入 n=3n=3,我们考虑所有长度为 33 的排列:

  • [1,2,3][1, 2, 3],所有子段均为“框定子段”(framed segment)。幸福值为 66。
  • [1,3,2][1, 3, 2],除子段 [1,2][1, 2] 外,其余所有子段均为“框定子段”。幸福值为 55。
  • [2,1,3][2, 1, 3],除子段 [2,3][2, 3] 外,其余所有子段均为“框定子段”。幸福值为 55。
  • [2,3,1][2, 3, 1],除子段 [2,3][2, 3] 外,其余所有子段均为“框定子段”。幸福值为 55。
  • [3,1,2][3, 1, 2],除子段 [1,2][1, 2] 外,其余所有子段均为“框定子段”。幸福值为 55。
  • [3,2,1][3, 2, 1],所有子段均为“框定子段”。幸福值为 66。

因此,幸福值总和为 6+5+5+5+5+6=326+5+5+5+5+6 = 32。

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

首页