CF1750F.Majority

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

Everyone was happy coding, until suddenly a power shortage happened and the best competitive programming site went down. Fortunately, a system administrator bought some new equipment recently, including some UPSs. Thus there are some servers that are still online, but we need all of them to be working in order to keep the round rated.

Imagine the servers being a binary string ss of length nn. If the ii-th server is online, then si=1s_i = 1, and si=0s_i = 0 otherwise.

A system administrator can do the following operation called electricity spread, that consists of the following phases:

  • Select two servers at positions 1≤i<j≤n1 \le i \lt j \le n such that both are online (i.e. si=sj=1s_i=s_j=1). The spread starts only from online servers.
  • Check if we have enough power to make the spread. We consider having enough power if the number of turned on servers in range [i,j][i, j] is at least the number of turned off servers in range [i,j][i, j]. More formally, check whether 2⋅(si+si+1+…+sj)≥j−i+12 \cdot (s_i + s_{i+1} + \ldots + s_j) \ge j - i + 1.
  • If the check is positive, turn on all the offline servers in range [i,j][i, j]. More formally, make sk:=1s_k := 1 for all kk from ii to jj.

We call a binary string ss of length nn rated if we can turn on all servers (i.e. make si=1s_i = 1 for 1≤i≤n1 \le i \le n) using the electricity spread operation any number of times (possibly, 00). Your task is to find the number of rated strings of length nn modulo mm.

所有人都在愉快地编程,直到突然发生停电,导致最佳的编程竞赛网站宕机了。幸运的是,一名系统管理员最近购买了一些新设备,其中包括若干台不间断电源(UPS)。因此,目前仍有部分服务器处于在线状态,但我们需要让所有服务器都恢复正常运行,才能保证本次比赛的评分有效。

将服务器状态建模为一个长度为 nn 的二进制字符串 ss:若第 ii 台服务器在线,则 si=1s_i = 1;否则 si=0s_i = 0。

系统管理员可以执行一种称为“电力扩散”(electricity spread)的操作,该操作包含以下步骤:

  • 选择两个位置 1≤i<j≤n1 \le i \lt j \le n 的服务器,且二者均在线(即满足 si=sj=1s_i = s_j = 1)。扩散仅能从已在线的服务器开始。
  • 检查是否有足够电力支持此次扩散。当区间 [i,j][i, j] 内已开启的服务器数量不少于已关闭的服务器数量时,视为电力充足。更准确地说,需满足:
    2⋅(si+si+1+…+sj)≥j−i+12 \cdot (s_i + s_{i+1} + \ldots + s_j) \ge j - i + 1。
  • 若上述条件成立,则将区间 [i,j][i, j] 内所有离线服务器全部开启。即对所有 k∈[i,j]k \in [i, j],令 sk:=1s_k := 1。

我们称一个长度为 nn 的二进制字符串 ss 是“可评分的”(rated),如果可以通过任意多次(包括零次)执行电力扩散操作,使得所有服务器均被开启(即对所有 1≤i≤n1 \le i \le n,均有 si=1s_i = 1)。你的任务是:计算长度为 nn 的可评分字符串的个数,并对 mm 取模。

输入格式

The first and only line contains two integers nn and mm (1≤n≤50001 \le n \le 5000, 10≤m≤10910 \le m \le 10^9) — the length of the string and the required module.

第一行且唯一一行包含两个整数 nn 和 mm(1≤n≤50001 \le n \le 5000,10≤m≤10910 \le m \le 10^9)—— 分别表示字符串的长度和所需的模数。

输出格式

Print a single integer — the number of rated binary strings of length nn. Since this number can be large, print it modulo mm.

输出一个整数——长度为 nn 的有评级二进制字符串的个数。由于该数可能很大,请对 mm 取模后输出。

输入输出样例

  • 输入#1

    2 100

    输出#1

    1
  • 输入#2

    3 10

    输出#2

    2
  • 输入#3

    4 3271890

    输出#3

    4
  • 输入#4

    17 123456

    输出#4

    32347

说明/提示

In the first example, the only rated string is 11. So the answer is 11.

In the second example, the rated strings are:

  • 111;
  • 101, because we can perform an operation with i=1i = 1 and j=3j = 3.

So the answer is 22.

In the third sample, the rated strings are:

  • 1001;
  • 1111;
  • 1011;

So the answer is 44.

在第一个例子中,唯一的达标字符串是 11。因此答案为 11。

在第二个例子中,达标字符串有:

  • 111;
  • 101,因为我们可对 i=1i = 1 和 j=3j = 3 执行一次操作。

因此答案为 22。

在第三个样例中,达标字符串有:

  • 1001;
  • 1111;
  • 1011;
  • 1101。

因此答案为 44。

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

首页