1281C - Cut and Paste - CodeForces Solution


implementation math *1700

Please click on ads to support us..

Python Code:

from collections import defaultdict, deque
import sys
input = lambda : sys.stdin.readline().strip()

md=1000000007
md2=998244353
mx=1000000000
mx2=int(1e18)


for t in range(int(input())):
    x=int(input())
    s = input()
    l=1
    while len(s) < x:
        s = s + (s[l:]*(int(s[l-1])-1))
        l += 1
    ms = len(s)
    
    while l <= x:
        ms = (ms-l)*(int(s[l-1]))+l
        ms %= md
        l+=1
    print(ms)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<< #x <<" : "<<(x)<<endl;
#define dbgv(v) cout<< #v <<" : ";for(auto &i:v) cout<<(i)<<" ";cout<<endl;
const int MOD = 1e9+7;
void solve()
// int solve()
{
    int x;cin>>x;
    string s;cin>>s;
    int n = s.size();
    int i = 0;
    while(s.size() < x){
        string t = s.substr(i+1,s.size() - i -1);
        for(int j=0;j<(s[i]-'1');j++)
        s += t;
        i++;
    }
    int ans = n;
    for(int i=1;i<=x;i++)
    {
        int ad = ((ans-i)*(s[i-1]-'1'))%MOD;
        (ans += ad)%=MOD;
    }
    (ans+=MOD)%=MOD;
    cout<<ans<<endl;
    
}
main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin>>t;
    while(t--)
    {
        solve();
        // cout<<solve()<<endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers