The first line of input contains a single integer () 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 (). The second line of each test case consists of the initial string (). It is guaranteed, that consists of the characters "1", "2", "3".
It is guaranteed that the sum of in a single file is at most . It is guaranteed that in each test case before the procedure will stop it will be true that at any time.
Output:
For each test case, output a single line containing a single integer denoting the answer for that test case modulo
.
- #include<bits/stdc++.h>
- #define M 1000000007
- using namespace std;
- int main()
- {
- int t;
- cin>>t;
- while(t--)
- {
- long long int x;
- string s,c,tmp="";
- cin>>x;
- cin>>s;
- long long int l=1;
- long long int a[x+1];
- a[0]=s.size()%M;
- for(long long int i=1;i<=x;++i)
- {
- a[i]=0;
- for(int j=0;j<(s[i-1]-'0');++j)
- a[i]=(a[i]+a[i-1]-i)%M;
- a[i]+=i;
- if(s.size()<=x)
- {
- tmp=s.substr(0,i);
- c=s.substr(i,s.size()-i);
- int l=s[i-1]-'0';
- while(l--)
- {
- tmp+=c;
- if(tmp.size()>=x)
- break;
- }
- s=tmp;
- }
- }
- cout<<a[x]%M<<endl;
- }
- return 0;
- }
Another trick for time limit:--
- #include<bits/stdc++.h>
- #define M 1000000007
- using namespace std;
- int main()
- {
- int t;
- cin>>t;
- while(t--)
- {
- long long int x;
- string _s,c,tmp="";
- cin>>x;
- cin>>(_s);
- unsigned long long int ls=(_s).size();
- vector <char> s(_s.begin(),_s.end());
- for (int i = 1; i <= x; ++i)
- {
- int v = s[i - 1] - '1';
- if (s.size() < x)
- {
- vector<char> sub(s.begin() + i, s.end());
- for (int it = 0; it < v; it++) s.insert(s.end(), sub.begin(), sub.end());
- }
- ls = (ls +( (ls - i) * v) % M);
- }
- cout<<(ls%M+M)%M<<endl;
- }
- return 0;
- }