#P3823. [NOI2017] 蚯蚓排队
[NOI2017] 蚯蚓排队
Description
There are earthworms in a kindergarten. For easier management, the principal Shen Dao Shou often asks the earthworms to line up and perform.
All earthworms are numbered with consecutive positive integers from to . The length of each earthworm is a positive integer, and by admission requirements, all lengths do not exceed . Shen Dao Shou wants these earthworms to form several queues. Initially, each earthworm forms a queue by itself, where that earthworm is both the head and the tail of its queue.
Shen Dao Shou will perform operations in sequence. Each operation is one of the following three types:
-
Given and , merge the two queues containing earthworms and into a single queue. Specifically, place earthworm immediately after earthworm , and keep the relative order of all other earthworms unchanged.
-
Given , split earthworm and the earthworm immediately after it into two queues. Specifically, after the split, earthworm becomes the tail of one queue, and the earthworm that was immediately after it becomes the head of the other queue. The relative order of all other earthworms remains unchanged.
-
Given a positive integer and a digit string of length at least , for every length- contiguous substring of (there are such substrings, where is the length of ), define a function and ask for the product of all these values modulo . The definition of is as follows:
For the current queues of earthworms, define an earthworm’s backward -digit string as follows: starting from that earthworm and moving along the queue towards the back, find the nearest earthworms (including itself) and concatenate their lengths as characters to form a digit string. If fewer than earthworms can be found this way, then it does not have a backward -digit string. For example, if the queue of earthworms has earthworm at the head, followed by earthworm , followed by earthworm (which is at the tail), and their lengths are , , and respectively, then the backward -digit string of earthworm is 456. Earthworm does not have a backward -digit string, but its backward -digit string is 56, and its backward -digit string is 5.
Then denotes the number of earthworms whose backward -digit string is exactly .
Input Format
The first line contains two positive integers , denoting the number of earthworms and the number of operations.
The second line contains positive integers not exceeding , in order, denoting the lengths of earthworms numbered .
Each of the next lines describes an operation, in one of the following formats:
-
1(): Merge the two queues containing earthworms and into one queue, and in the new queue, place earthworm immediately after earthworm . It is guaranteed that before this operation, earthworm is at the tail of some queue, earthworm is at the head of some queue, and the two earthworms are not in the same queue. -
2(): Split earthworm and the earthworm immediately after it into two queues. It is guaranteed that before this operation, earthworm is not the tail of its queue. -
3( is a positive integer, and is a digit string of length at least ): Query the product of over every length- substring of , modulo . The definition of is given in the problem statement.
Adjacent elements on the same line are separated by exactly one space.
The input file can be large. Please avoid overly slow input methods.
Output Format
For each operation of the form 3 s k, output a single line containing a single integer, which is the answer to that query.
5 9
3 1 3 5 3
3 333135 2
3 333135 1
1 1 3
1 2 5
1 3 2
1 5 4
3 333135 2
3 333135 1
3 333135 3
0
81
1
81
0
2 10
6 6
3 666666 1
1 1 2
3 666666 2
3 666666 4
3 666666666666666666666666666666 1
2 1
1 2 1
3 666666 2
3 666666 4
3 666666666666666666666666666666 1
64
1
0
75497471
1
0
75497471
Hint
Constraints:
It is guaranteed that , , .
Let be the total length of all query strings in an input file. Then .
Let be the number of operations of the form 2 i in an input file. Then .
Details for each test point are shown in the table below:
| Test Point | | | | | | All are | |:-:|:-:|:-:|:-:|:-:|:-:|:-:| | 1 | | | | | | No | | 2 | | | | | | No | | 3 | | | | | | No | | 4 | | | | | | No | | 5 | | | | | | No | | 6 | | | | | | No | | 7 | | | | | | Yes | | 8 | | | | | | No | | 9 | | | | | | No | | 10 | | | | | | No | | 11 | | | | | | No | | 12 | | | | | | No | | 13 | | | | | | Yes | | 14 | | | | | | No | | 15 | | | | | | No | | 16 | | | | | | No | | 17 | | | | | | No | | 18 | | | | | | No | | 19 | | | | | | No | | 20 | | | | | | No | | 21 | | | | | | Yes | | 22 | | | | | | No | | 23 | | | | | | No | | 24 | | | | | | No | | 25 | | | | | | No |
If the “All are 1” column of a test point is “Yes”, it means all earthworms have length , and every digit in every query string is also 1.
Translated by ChatGPT 5
京公网安备 11011102002149号