CF2189D1.Little String (Easy Version)

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

This is the easy version of the problem. The difference between the versions is that in this version, it is guaranteed that the string ss does not contain the character ?.

For the string w1w2…wnw_1w_2 \ldots w_n, consisting of characters 0 and 1, we define f(w)f(w) as the number of permutations p1,p2,…,pnp_1, p_2, \ldots, p_n of the array [0,1,…,n−1][0, 1, \ldots, n-1], such that for all ii from 11 to nn the following holds:

  • if wi=1w_i = \texttt{1}, then there exist such 1≤l≤r≤n1 \leq l \leq r \leq n that mex⁡([pl,pl+1,…,pr])=i\operatorname{mex}([p_l, p_{l+1}, \ldots, p_r]) = i;∗^{\text{∗}}

  • if wi=0w_i = \texttt{0}, then there do not exist such 1≤l≤r≤n1 \leq l \leq r \leq n that mex⁡([pl,pl+1,…,pr])=i\operatorname{mex}([p_l, p_{l+1}, \ldots, p_r]) = i.

Given a string s1s2…sns_1s_2 \ldots s_n, consisting of characters 0 and 1, and a positive integer cc. Note that in this version of the problem, the string ss doesn't contain ?. Consider all strings ww that can be obtained from ss by replacing all characters ? with characters 0 and 1. Find the smallest value of f(w)f(w) among all such strings ww that is not divisible by cc, or determine that such a string ww does not exist. Since the answer may be large, find it modulo 109+710^9+7.

∗^{\text{∗}}The minimum excluded (MEX) of a collection of integers c1,c2,…,ckc_1, c_2, \ldots, c_k is defined as the smallest non-negative integer xx which does not occur in the collection cc.

这是该问题的简单版本。两个版本的区别在于:在本版本中,保证字符串 ss 不包含字符 ?。

对于由字符 0 和 1 组成的字符串 w1w2…wnw_1w_2 \ldots w_n,我们定义 f(w)f(w) 为数组 [0,1,…,n−1][0, 1, \ldots, n-1] 的排列 p1,p2,…,pnp_1, p_2, \ldots, p_n 的个数,使得对所有 ii(从 11 到 nn)均满足以下条件:

  • 若 wi=1w_i = \texttt{1},则存在 1≤l≤r≤n1 \leq l \leq r \leq n,使得 mex⁡([pl,pl+1,…,pr])=i\operatorname{mex}([p_l, p_{l+1}, \ldots, p_r]) = i;∗^{\text{∗}}

  • 若 wi=0w_i = \texttt{0},则不存在 1≤l≤r≤n1 \leq l \leq r \leq n,使得 mex⁡([pl,pl+1,…,pr])=i\operatorname{mex}([p_l, p_{l+1}, \ldots, p_r]) = i。

给定一个由字符 0 和 1 组成的字符串 s1s2…sns_1s_2 \ldots s_n 和一个正整数 cc。注意,在本题版本中,字符串 ss 不含 ?。考虑所有可通过将 ss 中所有 ? 替换为 0 或 1 得到的字符串 ww。在所有这些字符串 ww 中,找出满足 f(w)f(w) 不被 cc 整除的最小 f(w)f(w) 值;若不存在这样的字符串 ww,则判定其不存在。由于答案可能很大,请对 109+710^9+7 取模输出结果。

∗^{\text{∗}} 一组整数 c1,c2,…,ckc_1, c_2, \ldots, c_k 的最小未出现值(MEX) 定义为不在该集合中出现的最小非负整数 xx。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and cc (3≤n≤2⋅1053 \leq n \leq 2 \cdot 10^5, 1≤c≤1091 \leq c \leq 10^9) — the length of the string and the number that limits the value of the function.

The second line of each test case contains a string of length nn, consisting of characters 0 and 1 — the string ss.

It is guaranteed that the sum of nn across all test cases does not exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 cc(3≤n≤2⋅1053 \leq n \leq 2 \cdot 10^5,1≤c≤1091 \leq c \leq 10^9)—— 分别表示字符串的长度以及限制函数值的数。

每个测试用例的第二行包含一个长度为 nn 的字符串,由字符 0 和 1 组成 —— 即字符串 ss。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, if there exists such a string ww that can be obtained from ss by replacing all characters ? with characters 0 and 1, such that f(w)f(w) is not divisible by cc, then output the minimum value of f(w)f(w) among all such strings ww. The answer should be output modulo 109+710^9+7. If such a string does not exist, output −1-1.

对于每个测试用例,若存在字符串 ww,使得 ww 可通过将 ss 中所有字符 ? 替换为字符 0 和 1 得到,且 f(w)f(w) 不能被 cc 整除,则输出所有满足条件的字符串 ww 中 f(w)f(w) 的最小值(答案对 109+710^9+7 取模)。若不存在这样的字符串,则输出 −1-1。

输入输出样例

  • 输入#1

    9
    3 3
    001
    3 1
    111
    4 100
    1001
    6 100
    111001
    6 100
    111101
    5 8
    10001
    4 100
    1110
    21 123456789
    111000111000111000111
    3 4
    101

    输出#1

    -1
    -1
    4
    96
    64
    12
    -1
    336892528
    2

说明/提示

In the second test case, there is no suitable string ww, since f(w)f(w) is always divisible by 11.

In the third test case, we can only take w=sw = s, then f(w)=4f(w) = 4, as there are exactly 44 suitable permutations:

  1. p=[0,2,3,1]p = [0, 2, 3, 1];
  2. p=[0,3,2,1]p = [0, 3, 2, 1];
  3. p=[1,2,3,0]p = [1, 2, 3, 0];
  4. p=[1,3,2,0]p = [1, 3, 2, 0].

In the sixth test case, one can take the string w=10001w = \mathtt{10001}, then f(w)f(w) will be equal to 1212. One of the suitable permutations is [0,4,3,2,1][0, 4, 3, 2, 1], while, for example, the permutation [0,1,2,3,4][0, 1, 2, 3, 4] does not fit. It can be shown that 1212 is the smallest value not divisible by 88 that can be obtained.

在第二个测试用例中,不存在合适的字符串 ww,因为 f(w)f(w) 总是能被 11 整除。

在第三个测试用例中,我们只能取 w=sw = s,此时 f(w)=4f(w) = 4,因为恰好存在 44 个合适的排列:

  1. p=[0,2,3,1]p = [0, 2, 3, 1];
  2. p=[0,3,2,1]p = [0, 3, 2, 1];
  3. p=[1,2,3,0]p = [1, 2, 3, 0];
  4. p=[1,3,2,0]p = [1, 3, 2, 0]。

在第六个测试用例中,可取字符串 w=10001w = \mathtt{10001},此时 f(w)=12f(w) = 12。其中一个合适的排列是 [0,4,3,2,1][0, 4, 3, 2, 1],而例如排列 [0,1,2,3,4][0, 1, 2, 3, 4] 就不符合要求。可以证明,1212 是能得到的、不被 88 整除的最小值。

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

首页