Encrypt the STRING

0

0 votes
Problem

You have a String of length N consisting of lowercase English letters. You have to encrypt the string using a sophisticated algorithm as follows:

1. There are Q passes.

2.In each pass, an index i  is arbitrarily chosen and another index j  (not necessarily different), is determined by the following pseudo-code which runs for t iterations where initially j=i  :

                            k=j

                           j=(aj+b)%N

                           b=k

3. The characters at index i and j are swapped at the end of each pass.

Find the encrypted string after Q passes.

Note: Large Input! Use Fast I/O.

INPUT

  • First line of input contains two space-separated integers, N ― the length of the string, and  Q ― the number of passes.
  • Next line contains a string of length N
  • Q lines follow, each containing four space-separated integers i,a,b  and t   describing the corresponding pass.

OUTPUT

Print a single line containing the encrypted String.

CONSTRAINTS

  • 1<=N,Q<=105
  • 0<=i<N
  • 0<=a,b<=109
  • 0<=t<=1e12
Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation
  1. At the end of the first pass, the string remains same.
  2. During second pass: i=4,a=1,b=2,t=3,j=4

            At the end of 1st iteration j=(14+2)%7=6,b=4

            At the end of 2nd iteration j=(16+4)%7=3,b=6

            At the end of 3rd iteration j=(13+6)%7=2,b=3

Hence, s[4] is swapped with s[2], changing sptraan to spartan .

Editor Image

?