Queue
Time limit1sMemory limit2048 MB
Worms with lengths 1 to 6 are merged and split in queues, and the task asks for the product of f(t) over all length-k substrings of s.
- Level
Hard9 of 10
- Topics
- Linked list, String matching, Implementation
- Solved
- No attempts yet
Problem
There are worms numbered from to . The length of each worm is a positive integer between and . You want to arrange the worms into several queues. Initially, each worm is alone in its own queue. Each worm is both the front and the end of its queue.
There are operations. Each operation is one of the following three types:
1 i j(): Merge the queue that contains worm with the queue that contains worm . In the new queue, worm immediately follows worm . Every other worm keeps its neighbors. It is guaranteed that worm is at the tail of its queue, worm is at the front of its queue, and and are not in the same queue.2 i(): Split the queue between worm and the worm right after it. It is guaranteed that worm is not at the tail of its queue.3 s k( is a positive integer and is a numeric string with length at least ): Find the product of modulo 998,244,353, taken over all substrings of length in . There are such substrings for given and .
The value is defined as follows.
In a given queue, the -string after worm is formed by starting at worm , walking toward the tail, and taking the worms closest to , counting itself. Their lengths are concatenated in order. If fewer than worms remain before the tail, worm has no -string. For example, suppose a queue holds worms 10, 22, 3 with lengths 4, 5, 6. The 3-string after worm 10 is 456. Worm 22 has no 3-string, but its 2-string is 56 and its 1-string is 5.
is the number of worms whose -string is exactly .
Input
The first line contains two positive integers and , the number of worms and the number of operations. The second line contains positive integers, each at most 6, giving the lengths of worms through . Each of the next lines describes one operation. The input file may be large.
Output
For each operation of type 3 s k, print the answer on its own line.
Constraints
, , .
Let be the sum of the lengths of over all queries. Then .
Let be the number of operations of the form 2 i. Then .