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)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值