CF2170E.Binary Strings and Blocks

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Define a block in a binary string (a string consisting of characters 0 and/or 1) as its continuous substring of characters of the same type that cannot be extended either to the left or to the right. For example, in the string 110001111, there are three blocks:

  • 11 (from the 11-st character to the 22-nd character);
  • 000 (from the 33-rd character to the 55-th character);
  • 1111 (from the 66-th character to the 99-th character).

The substring from the 77-th character to the 99-th character 111 is not a block because it can be extended to the left. The substring from the 11-st character to the 55-th character 11000 is not a block because it contains characters of different types.

We call a string beautiful if we can remove exactly one block from it so that the resulting string consists of an odd number of blocks. For example:

  • the string 110001111 is beautiful because we can remove the block from the 33-rd to the 55-th character, resulting in the string 111111, which consists of one block;
  • the string 1010 is beautiful because we can remove the block from the 11-st to the 11-st character, resulting in the string 010, which consists of three blocks;
  • the string 0000 is not beautiful because the only way to remove a block from it will result in an empty string, which consists of 00 blocks.

You are given an integer nn and mm constraints, the ii-th of which is described by a pair of integers li,ril_i, r_i. We denote the substring of the string ss from character ll to character rr inclusive as s[l:r]s[l:r], that is, s[l:r]=slsl+1…srs[l:r] = s_l s_{l+1} \dots s_r. Your task is to count the number of binary strings ss of length nn meeting the following condition:

  • for each ii from 11 to mm, the string s[li:ri]s[l_i:r_i] is beautiful.

在二进制字符串(即仅由字符 0 和/或 1 组成的字符串)中,我们定义一个块(block)为:该字符串中一个极大连续子串,其中所有字符均相同,且无法向左或向右延伸。例如,在字符串 110001111 中,存在三个块:

  • 11(从第 11 个字符到第 22 个字符);
  • 000(从第 33 个字符到第 55 个字符);
  • 1111(从第 66 个字符到第 99 个字符)。

子串 111(从第 77 个字符到第 99 个字符)不是一个块,因为它可以向左延伸;子串 11000(从第 11 个字符到第 55 个字符)也不是一个块,因为它包含不同类型(0 和 1)的字符。

我们称一个字符串是优美的(beautiful),如果从中恰好移除一个块后,所得字符串所含的块数为奇数。例如:

  • 字符串 110001111 是优美的,因为我们可以移除第 33 至第 55 个字符构成的块(即 000),得到字符串 111111,它仅含 1 个块;
  • 字符串 1010 是优美的,因为我们可以移除第 11 至第 11 个字符构成的块(即 1),得到字符串 010,它含有 3 个块;
  • 字符串 0000 不是优美的,因为从中移除唯一可能的一个块(即整个字符串 0000)后,结果为空字符串,它含有 00 个块(偶数)。

给定一个整数 nn 和 mm 个约束条件,其中第 ii 个约束由一对整数 li,ril_i, r_i 描述。我们记字符串 ss 从第 ll 个字符到第 rr 个字符(含端点)的子串为 s[l:r]s[l:r],即 s[l:r]=slsl+1…srs[l:r] = s_l s_{l+1} \dots s_r。你的任务是:统计满足以下条件的长度为 nn 的二进制字符串 ss 的个数:

  • 对每个 ii(1≤i≤m1 \le i \le m),子串 s[li:ri]s[l_i:r_i] 是优美的。

输入格式

The first line contains one integer tt (1≤t≤1041 \le t \le 10^4) — the number of test case.

The first line of each test case contains two integers nn and mm (2≤n≤3⋅1052 \le n \le 3 \cdot 10^5; 1≤m≤3⋅1051 \le m \le 3 \cdot 10^5) — the required length of the string and the number of constraints, respectively.

Next, there are mm lines, the ii-th of which contains two integers li,ril_i, r_i (1≤li<ri≤n1 \le l_i \lt r_i \le n) — the description of the ii-th constraint.

Additional constraints on the input:

  • The sum of nn across all test cases does not exceed 3⋅1053 \cdot 10^5;
  • The sum of mm across all test cases does not exceed 3⋅1053 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示测试用例的数量。

每个测试用例的第一行包含两个整数 nn 和 mm(2≤n≤3⋅1052 \le n \le 3 \cdot 10^5;1≤m≤3⋅1051 \le m \le 3 \cdot 10^5)—— 分别表示所要求的字符串长度和约束条件的数量。

接下来有 mm 行,其中第 ii 行包含两个整数 li,ril_i, r_i(1≤li<ri≤n1 \le l_i \lt r_i \le n)—— 描述第 ii 个约束条件。

输入的附加约束:

  • 所有测试用例中 nn 的总和不超过 3⋅1053 \cdot 10^5;
  • 所有测试用例中 mm 的总和不超过 3⋅1053 \cdot 10^5。

输出格式

For each test case, print one integer — the number of strings that satisfy the condition. Since it may be huge, output it modulo 998244353998244353.

对于每个测试用例,输出一个整数——满足条件的字符串数量。由于该数值可能很大,请对 998244353998244353 取模后输出。

输入输出样例

  • 输入#1

    3
    4 3
    1 2
    2 3
    3 4
    4 2
    1 2
    3 4
    200 1
    13 37

    输出#1

    2
    4
    570529459

说明/提示

In the first example from the statement, the following strings are suitable: 1010, 0101. For each of these strings, both s[1:2]s[1:2], s[2:3]s[2:3], and s[3:4]s[3:4] are beautiful.

在题目陈述的第一个例子中,以下字符串是符合条件的:1010、0101。对于每个这样的字符串,s[1:2]s[1:2]、s[2:3]s[2:3] 和 s[3:4]s[3:4] 均为优美的。

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

首页