CF1280A.Cut and Paste

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

We start with a string ss consisting only of the digits 11, 22, or 33. The length of ss is denoted by ∣s∣|s|. For each ii from 11 to ∣s∣|s|, the ii-th character of ss is denoted by sis_i.

There is one cursor. The cursor's location ℓ\ell is denoted by an integer in 0,…,∣s∣{0, \ldots, |s|}, with the following meaning:

  • If ℓ=0\ell = 0, then the cursor is located before the first character of ss.
  • If ℓ=∣s∣\ell = |s|, then the cursor is located right after the last character of ss.
  • If 0<ℓ<∣s∣0 \lt \ell \lt |s|, then the cursor is located between sℓs_\ell and sℓ+1s_{\ell+1}.

We denote by slefts_\text{left} the string to the left of the cursor and srights_\text{right} the string to the right of the cursor.

We also have a string cc, which we call our clipboard, which starts out as empty. There are three types of actions:

  • The Move action. Move the cursor one step to the right. This increments ℓ\ell once.
  • The Cut action. Set c←srightc \leftarrow s_\text{right}, then set s←slefts \leftarrow s_\text{left}.
  • The Paste action. Append the value of cc to the end of the string ss. Note that this doesn't modify cc.

The cursor initially starts at ℓ=0\ell = 0. Then, we perform the following procedure:

  1. Perform the Move action once.
  2. Perform the Cut action once.
  3. Perform the Paste action sℓs_\ell times.
  4. If ℓ=x\ell = x, stop. Otherwise, return to step 1.

You're given the initial string ss and the integer xx. What is the length of ss when the procedure stops? Since this value may be very large, only find it modulo 109+710^9 + 7.

It is guaranteed that ℓ≤∣s∣\ell \le |s| at any time.

我们从一个仅由数字 11、22 或 33 组成的字符串 ss 出发。ss 的长度记为 ∣s∣|s|。对每个从 11 到 ∣s∣|s| 的整数 ii,ss 的第 ii 个字符记为 sis_i。

有一个光标。光标的位置 ℓ\ell 是一个取值于 {0,…,∣s∣}\{0, \ldots, |s|\} 的整数,其含义如下:

  • 若 ℓ=0\ell = 0,则光标位于 ss 的第一个字符之前;
  • 若 ℓ=∣s∣\ell = |s|,则光标位于 ss 的最后一个字符之后;
  • 若 0<ℓ<∣s∣0 \lt \ell \lt |s|,则光标位于 sℓs_\ell 与 sℓ+1s_{\ell+1} 之间。

记 slefts_\text{left} 为光标左侧的子串,srights_\text{right} 为光标右侧的子串。

此外,我们还有一个字符串 cc,称为剪贴板(clipboard),初始为空。共有三种操作:

  • 移动(Move)操作:将光标向右移动一步,即 ℓ\ell 增加 11;
  • 剪切(Cut)操作:令 c←srightc \leftarrow s_\text{right},然后令 s←slefts \leftarrow s_\text{left};
  • 粘贴(Paste)操作:将 cc 的当前值追加到字符串 ss 的末尾(注意:该操作不改变 cc)。

光标初始位置为 ℓ=0\ell = 0。随后,我们执行以下过程:

  1. 执行一次 Move 操作;
  2. 执行一次 Cut 操作;
  3. 执行 sℓs_\ell 次 Paste 操作(注意:此处 sℓs_\ell 表示当前字符串 ss 中下标为 ℓ\ell 的字符所代表的数字,即若 sℓ=′2′s_\ell = '2',则执行 2 次 Paste);
  4. 若 ℓ=x\ell = x,则停止;否则返回步骤 1。

给定初始字符串 ss 和整数 xx,求该过程停止时 ss 的长度。由于该值可能极大,请只输出它对 109+710^9 + 7 取模的结果。

保证在任意时刻均有 ℓ≤∣s∣\ell \le |s|。

输入格式

The first line of input contains a single integer tt (1≤t≤10001 \le t \le 1000) denoting the number of test cases. The next lines contain descriptions of the test cases.

The first line of each test case contains a single integer xx (1≤x≤1061 \le x \le 10^6). The second line of each test case consists of the initial string ss (1≤∣s∣≤5001 \le |s| \le 500). It is guaranteed, that ss consists of the characters "1", "2", "3".

It is guaranteed that the sum of xx in a single file is at most 10610^6. It is guaranteed that in each test case before the procedure will stop it will be true that ℓ≤∣s∣\ell \le |s| at any time.

输入的第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000),表示测试用例的数量。接下来的若干行包含各测试用例的描述。

每个测试用例的第一行包含一个整数 xx(1≤x≤1061 \le x \le 10^6)。每个测试用例的第二行包含初始字符串 ss(1≤∣s∣≤5001 \le |s| \le 500)。保证字符串 ss 仅由字符 "1"、"2"、"3" 组成。

保证单个输入文件中所有测试用例的 xx 值之和不超过 10610^6。保证在每个测试用例中,程序停止前任意时刻均有 ℓ≤∣s∣\ell \le |s| 成立。

输出格式

For each test case, output a single line containing a single integer denoting the answer for that test case modulo 109+710^9 + 7.

对于每个测试用例,输出一行,包含一个整数,表示该测试用例的答案对 109+710^9 + 7 取模的结果。

输入输出样例

  • 输入#1

    4
    5
    231
    7
    2323
    6
    333
    24
    133321333

    输出#1

    25
    1438
    1101
    686531475

说明/提示

Let's illustrate what happens with the first test case. Initially, we have $s = $ 231. Initially, ℓ=0\ell = 0 and c=εc = \varepsilon (the empty string). The following things happen if we follow the procedure above:

  • Step 1, Move once: we get ℓ=1\ell = 1.

  • Step 2, Cut once: we get $s = $ 2 and $c = $ 31.

  • Step 3, Paste $s_\ell = $ 2 times: we get $s = $ 23131.

  • Step 4: ℓ=1≠x=5\ell = 1 \not= x = 5, so we return to step 1.

  • Step 1, Move once: we get ℓ=2\ell = 2.

  • Step 2, Cut once: we get $s = $ 23 and $c = $ 131.

  • Step 3, Paste $s_\ell = $ 3 times: we get $s = $ 23131131131.

  • Step 4: ℓ=2≠x=5\ell = 2 \not= x = 5, so we return to step 1.

  • Step 1, Move once: we get ℓ=3\ell = 3.

  • Step 2, Cut once: we get $s = $ 231 and $c = $ 31131131.

  • Step 3, Paste $s_\ell = $ 1 time: we get $s = $ 23131131131.

  • Step 4: ℓ=3≠x=5\ell = 3 \not= x = 5, so we return to step 1.

  • Step 1, Move once: we get ℓ=4\ell = 4.

  • Step 2, Cut once: we get $s = $ 2313 and $c = $ 1131131.

  • Step 3, Paste $s_\ell = $ 3 times: we get $s = $ 2313113113111311311131131.

  • Step 4: ℓ=4≠x=5\ell = 4 \not= x = 5, so we return to step 1.

  • Step 1, Move once: we get ℓ=5\ell = 5.

  • Step 2, Cut once: we get $s = $ 23131 and $c = $ 13113111311311131131.

  • Step 3, Paste $s_\ell = $ 1 times: we get $s = $ 2313113113111311311131131.

  • Step 4: ℓ=5=x\ell = 5 = x, so we stop.

At the end of the procedure, ss has length 2525.

我们以第一个测试用例为例说明整个过程。初始时,$s = $ 231,ℓ=0\ell = 0,且 c=εc = \varepsilon(空字符串)。若按上述步骤执行,将发生以下操作:

  • 第 1 步,移动一次:得到 ℓ=1\ell = 1。

  • 第 2 步,剪切一次:得到 $s = $ 2,$c = $ 31。

  • 第 3 步,粘贴 $s_\ell = $ 2 次:得到 $s = $ 23131。

  • 第 4 步:ℓ=1≠x=5\ell = 1 \not= x = 5,因此返回第 1 步。

  • 第 1 步,移动一次:得到 ℓ=2\ell = 2。

  • 第 2 步,剪切一次:得到 $s = $ 23,$c = $ 131。

  • 第 3 步,粘贴 $s_\ell = $ 3 次:得到 $s = $ 23131131131。

  • 第 4 步:ℓ=2≠x=5\ell = 2 \not= x = 5,因此返回第 1 步。

  • 第 1 步,移动一次:得到 ℓ=3\ell = 3。

  • 第 2 步,剪切一次:得到 $s = $ 231,$c = $ 31131131。

  • 第 3 步,粘贴 $s_\ell = $ 1 次:得到 $s = $ 23131131131。

  • 第 4 步:ℓ=3≠x=5\ell = 3 \not= x = 5,因此返回第 1 步。

  • 第 1 步,移动一次:得到 ℓ=4\ell = 4。

  • 第 2 步,剪切一次:得到 $s = $ 2313,$c = $ 1131131。

  • 第 3 步,粘贴 $s_\ell = $ 3 次:得到 $s = $ 2313113113111311311131131。

  • 第 4 步:ℓ=4≠x=5\ell = 4 \not= x = 5,因此返回第 1 步。

  • 第 1 步,移动一次:得到 ℓ=5\ell = 5。

  • 第 2 步,剪切一次:得到 $s = $ 23131,$c = $ 13113111311311131131。

  • 第 3 步,粘贴 $s_\ell = $ 1 次:得到 $s = $ 2313113113111311311131131。

  • 第 4 步:ℓ=5=x\ell = 5 = x,因此停止。

该过程结束时,ss 的长度为 2525。

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

首页