Cut and Paste(模拟)
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 sisi.
There is one cursor. The cursor’s location ?? is denoted by an integer in {0,…,|s|}{0,…,|s|}, with the following meaning:
If ?=0?=0, then the cursor is located before the first character of ss.
If ?=|s|?=|s|, then the cursor is located right after the last character of ss.
If 0<?<|s|0<?<|s|, then the cursor is located between s?s? and s?+1s?+1.
We denote by sleftsleft the string to the left of the cursor and srightsright 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 ?? once.
The Cut action. Set c←srightc←sright, then set s←slefts←sleft.
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?=0. Then, we perform the following procedure:
Perform the Move action once.
Perform the Cut action once.
Perform the Paste action s?s? times.
If ?=x?=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+7109+7.
It is guaranteed that ?≤|s|?≤|s| at any time.
Input
The first line of input contains a single integer tt (1≤t≤10001≤t≤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≤x≤106). The second line of each test case consists of the initial string ss (1≤|s|≤5001≤|s|≤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 106106. It is guaranteed that in each test case before the procedure will stop it will be true that ?≤|s|?≤|s| at any time.
Output
For each test case, output a single line containing a single integer denoting the answer for that test case modulo 109+7109+7.
Example
Input
4
5
231
7
2323
6
333
24
133321333
Output
25
1438
1101
686531475
Note
Let’s illustrate what happens with the first test case. Initially, we have s=s= 231. Initially, ?=0?=0 and c=εc=ε (the empty string). The following things happen if we follow the procedure above:
Step 1, Move once: we get ?=1?=1.
Step 2, Cut once: we get s=s= 2 and c=c= 31.
Step 3, Paste s?=s?= 2 times: we get s=s= 23131.
Step 4: ?=1≠x=5?=1≠x=5, so we return to step 1.
Step 1, Move once: we get ?=2?=2.
Step 2, Cut once: we get s=s= 23 and c=c= 131.
Step 3, Paste s?=s?= 3 times: we get s=s= 23131131131.
Step 4: ?=2≠x=5?=2≠x=5, so we return to step 1.
Step 1, Move once: we get ?=3?=3.
Step 2, Cut once: we get s=s= 231 and c=c= 31131131.
Step 3, Paste s?=s?= 1 time: we get s=s= 23131131131.
Step 4: ?=3≠x=5?=3≠x=5, so we return to step 1.
Step 1, Move once: we get ?=4?=4.
Step 2, Cut once: we get s=s= 2313 and c=c= 1131131.
Step 3, Paste s?=s?= 3 times: we get s=s= 2313113113111311311131131.
Step 4: ?=4≠x=5?=4≠x=5, so we return to step 1.
Step 1, Move once: we get ?=5?=5.
Step 2, Cut once: we get s=s= 23131 and c=c= 13113111311311131131.
Step 3, Paste s?=s?= 1 times: we get s=s= 2313113113111311311131131.
Step 4: ?=5=x?=5=x, so we stop.
At the end of the procedure, ss has length 2525.
首先把初始的字符串長度弄到比x大,然后就按照題目要求去計算就行。但是有一個坑點,就是s字符串的長度要按照取余之后計算,不能按照本來應該有的長度計算,這是一個坑點。我用了快速乘,但是好像不用也可以。。
代碼如下:
努力加油a啊,(o)/~
總結
以上是生活随笔為你收集整理的Cut and Paste(模拟)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: New Year and Ascent
- 下一篇: Yet Another Meme Pro