luogu#P5181. [COCI 2009/2010 #1] GENIJALAC
[COCI 2009/2010 #1] GENIJALAC
Problem Description
Translated from COCI 2009.10 T5 “GENIJALAC”.
Mirko invented a shuffling machine. The machine takes a paper tape with columns and infinitely many rows as both input and output. These columns are numbered in order.
At the beginning, only the first row of the tape contains numbers, and every row below it is blank.
In the first row, each column contains an integer: column contains the integer .
In addition, Mirko is given a shuffle permutation, which is a permutation of length : .
There is a row pointer, which initially points to the first row.
Each time the machine runs, for every , it moves the number in column of the current row (the row pointed to by the pointer) to column of the next row. After all numbers are placed, the pointer moves down by one row.
Mirko runs the machine infinitely many times. Now Mirko cuts off the first columns and the last columns of the tape; we call the result the new tape.
Question: among rows of the new tape, how many rows are identical to the first row of the new tape?
Input Format
The first line contains five integers .
The second line contains integers .
Output Format
One line with one integer: the number of rows among rows of the new tape that are identical to the first row of the new tape.
4 1 5 0 1
1 3 4 2
2
7 3 8 1 2
2 3 1 6 4 7 5
0
6 2 11 3 0
6 3 5 4 2 1
1
Hint
Sample Explanation 1
+-----+
|1 2 3|4 <--
|1 3 4|2
|1 4 2|3
|1 2 3|4 <--
|1 3 4|2
+-----+
1 4 2 3
1 2 3 4
Sample Explanation 2
1 2 3 4 5 6 7
2 3 1 6 4 7 5
+-------+
3|1 2 7 6|5 4
1|2 3 5 7|4 6
2|3 1 4 5|6 7
3|1 2 6 4|7 5
1|2 3 7 6|5 4
2|3 1 5 7|4 6
+-------+
3 1 2 4 5 6 7
1 2 3 6 4 7 5
Sample Explanation 3
1 2 3 4 5 6
+-----+
6 3 5|4 2 1|
1 5 2|4 3 6|
6 2 3|4 5 1|
1 3 5|4 2 6|
6 5 2|4 3 1|
1 2 3|4 5 6| <--
6 3 5|4 2 1|
1 5 2|4 3 6|
6 2 3|4 5 1|
1 3 5|4 2 6|
+-----+
Constraints and Hints
For of the testdata, .
For all testdata, and .
Translated by ChatGPT 5