CF1280A.Cut and Paste

普及/提高-

通过率:0%

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<<s0 < \ell < |s| , then the cursor is located between ss_\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 csrightc \leftarrow s_\text{right} , then set sslefts \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 ss_\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.

输入格式

The first line of input contains a single integer tt ( 1t10001 \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 ( 1x1061 \le x \le 10^6 ). The second line of each test case consists of the initial string ss ( 1s5001 \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.

输出格式

For each test case, output a single line containing a single integer denoting the answer for that test case modulo 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: =1x=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: =2x=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: =3x=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: =4x=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 .

首页