CF1439D.INOI Final Contests

NOI/NOI+/CTSC

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Today is the final contest of INOI (Iranian National Olympiad in Informatics). The contest room is a row with nn computers. All computers are numbered with integers from 11 to nn from left to right. There are mm participants, numbered with integers from 11 to mm.

We have an array aa of length mm where aia_{i} (1≤ai≤n1 \leq a_i \leq n) is the computer behind which the ii-th participant wants to sit.

Also, we have another array bb of length mm consisting of characters 'L' and 'R'. bib_i is the side from which the ii-th participant enters the room. 'L' means the participant enters from the left of computer 11 and goes from left to right, and 'R' means the participant enters from the right of computer nn and goes from right to left.

The participants in the order from 11 to mm enter the room one by one. The ii-th of them enters the contest room in the direction bib_i and goes to sit behind the aia_i-th computer. If it is occupied he keeps walking in his direction until he reaches the first unoccupied computer. After that, he sits behind it. If he doesn't find any computer he gets upset and gives up on the contest.

The madness of the ii-th participant is the distance between his assigned computer (aia_i) and the computer he ends up sitting behind. The distance between computers ii and jj is equal to ∣i−j∣|i - j|.

The values in the array aa can be equal. There exist nm⋅2mn^m \cdot 2^m possible pairs of arrays (a,b)(a, b).

Consider all pairs of arrays (a,b)(a, b) such that no person becomes upset. For each of them let's calculate the sum of participants madnesses. Find the sum of all these values.

You will be given some prime modulo pp. Find this sum by modulo pp.

今天是伊朗信息学国家奥林匹克竞赛(INOI)的决赛。比赛场地是一排共 nn 台计算机,从左到右依次编号为 11 到 nn。共有 mm 名参赛者,编号为 11 到 mm。

我们有一个长度为 mm 的数组 aa,其中 aia_i(1≤ai≤n1 \leq a_i \leq n)表示第 ii 名参赛者希望坐在其后的那台计算机编号。

此外,我们还有一个长度为 mm 的数组 bb,其每个元素为字符 'L' 或 'R'。bib_i 表示第 ii 名参赛者进入赛场的方向:'L' 表示该参赛者从计算机 11 的左侧进入,并从左向右行进;'R' 表示该参赛者从计算机 nn 的右侧进入,并从右向左行进。

参赛者按编号顺序 11 到 mm 依次入场。第 ii 名参赛者按方向 bib_i 进入赛场,并朝其目标计算机 aia_i 行进。若该计算机已被占用,则他继续沿原方向行走,直到遇到第一台空闲计算机,然后坐在其后。若途中未找到任何空闲计算机,则他感到沮丧并放弃比赛。

第 ii 名参赛者的“疯狂值”定义为其目标计算机(即 aia_i)与其最终所坐计算机之间的距离。计算机 ii 与 jj 之间的距离为 ∣i−j∣|i - j|。

数组 aa 中的值可以重复。所有可能的数组对 (a,b)(a, b) 共有 nm⋅2mn^m \cdot 2^m 种。

考虑所有满足“无人沮丧放弃”的数组对 (a,b)(a, b)。对每一对这样的 (a,b)(a, b),计算所有参赛者疯狂值之和。求出所有这些和的总和。

题目将给出一个质数模数 pp。请输出该总和对 pp 取模的结果。

输入格式

The only line contains three integers nn, mm, pp (1≤m≤n≤500,108≤p≤109+91 \leq m \leq n \leq 500, 10^8 \leq p \leq 10 ^ 9 + 9).

It is guaranteed, that the number pp is prime.

唯一一行包含三个整数 nn、mm、pp(1≤m≤n≤5001 \leq m \leq n \leq 500,108≤p≤109+910^8 \leq p \leq 10 ^ 9 + 9)。

保证 pp 是一个质数。

输出格式

Print only one integer — the required sum by modulo pp.

仅输出一个整数——对 pp 取模后的所需和。

输入输出样例

  • 输入#1

    3 1 1000000007

    输出#1

    0
  • 输入#2

    2 2 1000000009

    输出#2

    4
  • 输入#3

    3 2 998244353

    输出#3

    8
  • 输入#4

    20 10 1000000009

    输出#4

    352081045

说明/提示

In the first test, there are three possible arrays aa: 1{1}, 2{2}, and $ {3}$ and two possible arrays bb: L{\mathtt{L}} and R{\mathtt{R}}. For all six pairs of arrays (a,b)(a, b), the only participant will sit behind the computer a1a_1, so his madness will be 00. So the total sum of madnesses will be 00.

In the second test, all possible pairs of arrays (a,b)(a, b), such that no person becomes upset are:

  • (1,1,L,L)({1, 1}, {\mathtt{L}, \mathtt{L}}), the sum of madnesses is 11;
  • (1,1,R,L)({1, 1}, {\mathtt{R}, \mathtt{L}}), the sum of madnesses is 11;
  • (2,2,R,R)({2, 2}, {\mathtt{R}, \mathtt{R}}), the sum of madnesses is 11;
  • (2,2,L,R)({2, 2}, {\mathtt{L}, \mathtt{R}}), the sum of madnesses is 11;
  • all possible pairs of a∈1,2,2,1a \in {{1, 2}, {2, 1}} and b∈L,L,R,L,L,R,R,Rb \in {{\mathtt{L}, \mathtt{L}}, {\mathtt{R}, \mathtt{L}}, {\mathtt{L}, \mathtt{R}}, {\mathtt{R}, \mathtt{R}}}, the sum of madnesses is 00.

So, the answer is 1+1+1+1+0…=41 + 1 + 1 + 1 + 0 \ldots = 4.

在第一个测试用例中,有三种可能的数组 aa:1{1}、2{2} 和 3{3},以及两种可能的数组 bb:L{\mathtt{L}} 和 R{\mathtt{R}}。对于全部六对数组 (a,b)(a, b),唯一一名参与者都将坐在计算机 a1a_1 后方,因此他的疯狂值为 00。故所有疯狂值的总和为 00。

在第二个测试用例中,所有满足“无人感到不满”的数组对 (a,b)(a, b) 如下:

  • (1,1,L,L)({1, 1}, {\mathtt{L}, \mathtt{L}}),疯狂值之和为 11;
  • (1,1,R,L)({1, 1}, {\mathtt{R}, \mathtt{L}}),疯狂值之和为 11;
  • (2,2,R,R)({2, 2}, {\mathtt{R}, \mathtt{R}}),疯狂值之和为 11;
  • (2,2,L,R)({2, 2}, {\mathtt{L}, \mathtt{R}}),疯狂值之和为 11;
  • 所有可能的 a∈1,2,2,1a \in {{1, 2}, {2, 1}} 与 b∈L,L,R,L,L,R,R,Rb \in {{\mathtt{L}, \mathtt{L}}, {\mathtt{R}, \mathtt{L}}, {\mathtt{L}, \mathtt{R}}, {\mathtt{R}, \mathtt{R}}} 的组合,疯狂值之和均为 00。

因此,答案为 1+1+1+1+0…=41 + 1 + 1 + 1 + 0 \ldots = 4。

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

首页