CF1704H1.Game of AI (easy version)

NOI/NOI+/CTSC

通过率:0%

时间限制:2.50s

内存限制:1024MB

AC君温馨提醒

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

题目描述

This is the easy version of this problem. The difference between easy and hard versions is the constraint on kk and the time limit. Also, in this version of the problem, you only need to calculate the answer when n=kn=k. You can make hacks only if both versions of the problem are solved.

Cirno is playing a war simulator game with nn towers (numbered from 11 to nn) and nn bots (numbered from 11 to nn). The ii-th tower is initially occupied by the ii-th bot for 1≤i≤n1 \le i \le n.

Before the game, Cirno first chooses a permutation p=[p1,p2,…,pn]p = [p_1, p_2, \ldots, p_n] of length nn (A permutation of length nn is an array of length nn where each integer between 11 and nn appears exactly once). After that, she can choose a sequence a=[a1,a2,…,an]a = [a_1, a_2, \ldots, a_n] (1≤ai≤n1 \le a_i \le n and ai≠ia_i \ne i for all 1≤i≤n1 \le i \le n).

The game has nn rounds of attacks. In the ii-th round, if the pip_i-th bot is still in the game, it will begin its attack, and as the result the apia_{p_i}-th tower becomes occupied by the pip_i-th bot; the bot that previously occupied the apia_{p_i}-th tower will no longer occupy it. If the pip_i-th bot is not in the game, nothing will happen in this round.

After each round, if a bot doesn't occupy any towers, it will be eliminated and leave the game. Please note that no tower can be occupied by more than one bot, but one bot can occupy more than one tower during the game.

At the end of the game, Cirno will record the result as a sequence b=[b1,b2,…,bn]b = [b_1, b_2, \ldots, b_n], where bib_i is the number of the bot that occupies the ii-th tower at the end of the game.

However, as a mathematics master, she wants you to solve the following counting problem instead of playing games:

Count the number of different pairs of sequences aa and bb that we can get from all possible choices of sequence aa and permutation pp.

Since this number may be large, output it modulo MM.

这是本题的简单版本。简单版与困难版的区别在于对 kk 的约束以及时间限制。此外,在本题的这一版本中,你只需计算当 n=kn=k 时的答案。仅当两个版本均被解决时,才允许进行 hack。

Cirno 正在玩一款战争模拟游戏,游戏中有 nn 座塔(编号为 11 到 nn)和 nn 个机器人(编号为 11 到 nn)。初始时,第 ii 座塔由第 ii 个机器人占据(其中 1≤i≤n1 \le i \le n)。

游戏开始前,Cirno 首先选择一个长度为 nn 的排列 p=[p1,p2,…,pn]p = [p_1, p_2, \ldots, p_n](长度为 nn 的排列是指一个长度为 nn 的数组,其中 11 到 nn 的每个整数恰好出现一次)。之后,她可以选择一个序列 a=[a1,a2,…,an]a = [a_1, a_2, \ldots, a_n](满足 1≤ai≤n1 \le a_i \le n 且对所有 1≤i≤n1 \le i \le n 均有 ai≠ia_i \ne i)。

游戏共进行 nn 轮攻击。在第 ii 轮中,若第 pip_i 个机器人仍在游戏中,则它将发起攻击,结果是第 apia_{p_i} 座塔被第 pip_i 个机器人占据;原先占据第 apia_{p_i} 座塔的机器人将不再占据该塔。若第 pip_i 个机器人已不在游戏中,则本轮不发生任何事情。

每轮结束后,若某个机器人未占据任何塔,则该机器人将被淘汰并退出游戏。请注意:任意一座塔至多被一个机器人占据,但一个机器人在游戏过程中可以占据多座塔。

游戏结束后,Cirno 将记录结果为一个序列 b=[b1,b2,…,bn]b = [b_1, b_2, \ldots, b_n],其中 bib_i 表示游戏结束时占据第 ii 座塔的机器人的编号。

然而,作为一名数学大师,她希望你解决如下计数问题,而非实际玩游戏:

统计所有可能的序列 aa 和排列 pp 所能产生的不同的 (a,b)(a, b) 序列对的总数。

由于该数目可能很大,请输出其对 MM 取模的结果。

输入格式

The only line contains two positive integers kk and MM (1≤k≤50001\le k\le 5000, 2≤M≤1092\le M\le 10^9 ). It is guaranteed that 2182^{18} is a divisor of M−1M-1 and MM is a prime number.

You need to calculate the answer for n=kn=k.

唯一的一行包含两个正整数 kk 和 MM(1≤k≤50001\le k\le 5000,2≤M≤1092\le M\le 10^9)。保证 2182^{18} 是 M−1M-1 的约数,且 MM 是一个质数。

你需要计算 n=kn=k 时的答案。

输出格式

Output a single integer — the number of different pairs of sequences for n=kn=k modulo MM.

输出一个整数——当 n=kn=k 时,不同序列对的数目对 MM 取模的结果。

输入输出样例

  • 输入#1

    1 998244353

    输出#1

    0
  • 输入#2

    2 998244353

    输出#2

    2
  • 输入#3

    3 998244353

    输出#3

    24
  • 输入#4

    8 998244353

    输出#4

    123391016

说明/提示

For n=1n=1, no valid sequence aa exists. We regard the answer as 00.

For n=2n=2, there is only one possible array aa: [2,1][2, 1].

  • For array aa is [2,1][2, 1] and permutation pp is [1,2][1, 2], the sequence bb will be [1,1][1, 1] after all rounds have finished. The details for each rounds:
    • In the first round, the first bot will begin its attack and successfully capture the tower 22. After this round, the second bot will be eliminated and leave the game as all of its towers are occupied by other bots.
    • In the second round, the second bot is not in the game.
  • For array aa is [2,1][2, 1] and permutation pp is [2,1][2, 1], the sequence bb will be [2,2][2, 2] after all rounds have finished. The details for each rounds:
    • In the first round, the second bot will begin its attack and successfully capture the tower 11. After this round, the first bot will be eliminated and leave the game as all of its towers are occupied by other bots.
    • In the second round, the first bot is not in the game.

So the number of different pairs of sequences (a,b)(a,b) is 22 ([2,1][2, 1], [1,1][1, 1] and [2,1][2, 1], [2,2][2, 2]) for n=2n=2.

当 n=1n=1 时,不存在合法的序列 aa,此时答案定义为 00。

当 n=2n=2 时,唯一可能的数组 aa 是:[2,1][2, 1]。

  • 当数组 a=[2,1]a = [2, 1]、排列 p=[1,2]p = [1, 2] 时,所有轮次结束后序列 bb 为 [1,1][1, 1]。各轮次细节如下:
    • 第一轮中,第一个机器人开始攻击,并成功占领塔 22。本轮结束后,第二个机器人被淘汰并退出游戏,因为其所有塔均已被其他机器人占领。
    • 第二轮中,第二个机器人已不在游戏中。
  • 当数组 a=[2,1]a = [2, 1]、排列 p=[2,1]p = [2, 1] 时,所有轮次结束后序列 bb 为 [2,2][2, 2]。各轮次细节如下:
    • 第一轮中,第二个机器人开始攻击,并成功占领塔 11。本轮结束后,第一个机器人被淘汰并退出游戏,因为其所有塔均已被其他机器人占领。
    • 第二轮中,第一个机器人已不在游戏中。

因此,对于 n=2n=2,不同的序列对 (a,b)(a,b) 共有 22 个(即 ([2,1],[1,1])([2, 1], [1, 1]) 和 ([2,1],[2,2])([2, 1], [2, 2]))。

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

首页