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字符串的长度要按照取余之后计算,不能按照本来应该有的长度计算,这是一个坑点。我用了快速乘,但是好像不用也可以。。
代码如下:

#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;

string s;
int x;

inline ll qsc(ll a,int b)
{
	ll ans=0;
	while(b)
	{
		if(b&1) ans=(ans+a)%mod;
		a=(a+a)%mod;
		b>>=1;
	}
	return ans;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		cin>>x>>s;
		ll ans=s.length();
		for(int i=1;s.length()<x;i++)
		{
			string c=s.substr(i,s.length()-1);
			for(int j=1;j<=s[i-1]-'0'-1;j++) s+=c;
		}
		ll len1=ans,len2=0;
		for(int i=1;i<=x;i++)
		{
			int y=s[i-1]-'0';
			len2=ans-(ll)i;
			ans=((ans+qsc(len2,y-1)%mod)%mod+mod)%mod;
			len1+=((ll)(y-1)*len2);
		}
		cout<<ans%mod<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

发布了414 篇原创文章 · 获赞 23 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/104110108
cut