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

Cut and Paste(模拟)相关推荐

  1. 几个常用的文本处理shell 命令:find、grep、xargs、sort、uniq、tr、cut、paste、wc、sed、awk

    内容目录: find 文件查找 grep 文本搜索 xargs 命令行参数转换 sort 排序 uniq 消除重复行 用tr进行转换 cut 按列切分文本 paste 按列拼接文本 wc 统计行和字符 ...

  2. 你不知道的copy、cut、paste事件——实现页面内容禁止复制,实现复制内容添加额外字段,比如复制请注明出处

    聊聊copy复制.cut剪切.paste粘贴事件. 首先这三个事件都属于ClipboardEvent事件(剪切板事件).什么意思呢?这个三个事件的事件对象event其实都是ClipboardEvent ...

  3. linux(二)—cut,tee,paste,tr工具的用法和案例

    1:linux cut命令作用:用来截取分割文本和执行命令结果中的某些内容 2:语法:cut  参数  文件 3:查看file3.txt的文件内容 参数: -c :以字符为单位进行分割. 解释:在文件 ...

  4. java中cut是什么意思_cut和paste用法

    cut用来从标准输入或文本文件中剪切列或域.剪切文本可以将之粘贴到一个文本文件 ============================================================ ...

  5. JAVA模拟win7记事本

     使用JAVA模拟记事本 学校老师叫用java swing写一个记事本,虽然java窗体不是java的强项,但还是可以作为练习熟悉一下的. 因为记事本这种单一的小东西,所以不需要什么耦合不需要太低,虽 ...

  6. sort cut 命令的常用用法

    sort命令介绍: sort是在Linux里非常常用的一个命令,管排序的,集中精力,五分钟搞定sort,现在开始! 1 sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比较原则是从首 ...

  7. Linux上常用命令整理(二)—— paste

    上一篇整理了cat指令的几个基本常见用法,这次整理一下paste指令的基本用法. cat paste cut grep paste paste可以简单的理解为把两个文件的内容按列合并,与cat命令直接 ...

  8. tinyMce在线编辑器内JavaScript实现按Ctrl+S无刷新保存

    以前也用过几个编辑器拉,如FCK,CUTEDTOR等,它们大都实现了很不错了的功能,也有丰富的插件使用.不过我还是觉得TinyMCE在线编辑器好用,它是采用纯JS客户端脚本技术构建,是一个轻量级加载速 ...

  9. javascript技巧收集(200多个)---转

    1.文本框焦点问题 onBlur:当失去输入焦点后产生该事件 onFocus:当输入获得焦点后,产生该文件 Onchange:当文字值改变时,产生该事件 Onselect:当文字加亮后,产生该文件 & ...

最新文章

  1. 为VMware虚拟机中的Linux系统设置固定IP的方法
  2. scikit_learn中fit()/transform()/fit_transform()区别和联系
  3. jquery中ajax完整例子get,jq的ajax方法,jquery中ajax完整例子
  4. OpenShift 4 - Istio-Tutorial (1) 教程说明和准备环境
  5. vSphere 故障排除之工具篇
  6. asc怎么用 linux zip_linux的asc文件怎么打开
  7. 深度学习网络架构(三):VGG
  8. 格力可以考虑收购一个手机品牌
  9. spss essentials for python_SPSS python教程:[1]安装Python Essentials
  10. java如何准确的读取多音字
  11. 屏幕小于6英寸的手机_2019小屏手机有哪些 8款6英寸以下小屏全面屏手机推荐 (全文)...
  12. html中onfocus作用,HTML onfocus用法及代码示例
  13. android 修改系统默认时间24小时制
  14. HDU - 4532 湫秋系列故事――安排座位
  15. 正则表达式贪婪模式、懒惰模式与独占模式浅析
  16. Excel技能培训之十六自动高亮重复值,满足条件的值,自定义高亮条件,自动高亮前三名,显示数据条,色阶
  17. Unity3D 无限滚动列表
  18. 你为什么选择成为一个程序员,你适合做一个软件开发工程师吗?
  19. 在线隐藏字符加密/解密工具
  20. replace、replaceAll、replaceFirst的区别

热门文章

  1. python flask框架剖析_python flask框架实现传数据到js的方法分析
  2. 记录几款比较好用的jquery插件
  3. 微信小程序之通过Canvas生成图片保存到手机相册
  4. 内存泄漏的原因及解决办法_编程基础 | C++片段 指针、多态和内存分配
  5. Android开发之View双指缩放ViewGroup双指缩放视频双指缩放图片双指缩放
  6. 西部数据硬盘支持linux,西部数据开发新的Linux文件系统“Zonefs” 支持在分区块设备上运行...
  7. php ttf 字体 url,window_TTF字体文件如何安装 TTF文件打不开解决方法,TTF文件是Windows系统下的字体文 - phpStudy...
  8. 让线程等待10秒_把python程序变成多线程
  9. Pad和Margin
  10. AJPFX讲解Java 性能优化[4]:关于 finalize 函数