CF1704H1.Game of AI (easy version)
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is the easy version of this problem. The difference between easy and hard versions is the constraint on k and the time limit. Also, in this version of the problem, you only need to calculate the answer when n=k . You can make hacks only if both versions of the problem are solved.
Cirno is playing a war simulator game with n towers (numbered from 1 to n ) and n bots (numbered from 1 to n ). The i -th tower is initially occupied by the i -th bot for 1≤i≤n .
Before the game, Cirno first chooses a permutation p=[p1,p2,…,pn] of length n (A permutation of length n is an array of length n where each integer between 1 and n appears exactly once). After that, she can choose a sequence a=[a1,a2,…,an] ( 1≤ai≤n and ai=i for all 1≤i≤n ).
The game has n rounds of attacks. In the i -th round, if the pi -th bot is still in the game, it will begin its attack, and as the result the api -th tower becomes occupied by the pi -th bot; the bot that previously occupied the api -th tower will no longer occupy it. If the pi -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] , where bi is the number of the bot that occupies the i -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 a and b that we can get from all possible choices of sequence a and permutation p .
Since this number may be large, output it modulo M .
输入格式
The only line contains two positive integers k and M ( 1≤k≤5000 , 2≤M≤109 ). It is guaranteed that 218 is a divisor of M−1 and M is a prime number.
You need to calculate the answer for n=k .
输出格式
Output a single integer — the number of different pairs of sequences for n=k modulo M .
输入输出样例
输入#1
1 998244353
输出#1
0
输入#2
2 998244353
输出#2
2
输入#3
3 998244353
输出#3
24
输入#4
8 998244353
输出#4
123391016
说明/提示
For n=1 , no valid sequence a exists. We regard the answer as 0 .
For n=2 , there is only one possible array a : [2,1] .
- For array a is [2,1] and permutation p is [1,2] , the sequence b will be [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 2 . 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 a is [2,1] and permutation p is [2,1] , the sequence b will be [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 1 . 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) is 2 ( [2,1] , [1,1] and [2,1] , [2,2] ) for n=2 .