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 NN columns and infinitely many rows as both input and output. These NN columns are numbered 1N1 \ldots N 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 ii contains the integer ii.
In addition, Mirko is given a shuffle permutation, which is a permutation of length NN: s1,s_1, s2,s_2, ,\ldots, sNs_N.

There is a row pointer, which initially points to the first row.
Each time the machine runs, for every ii, it moves the number in column ii of the current row (the row pointed to by the pointer) to column sis_i of the next row. After all NN numbers are placed, the pointer moves down by one row.

Mirko runs the machine infinitely many times. Now Mirko cuts off the first CC columns and the last DD columns of the tape; we call the result the new tape.

Question: among rows ABA \sim B 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 N,A,B,C,DN, A, B, C, D.
The second line contains NN integers s1,s_1, s2,s_2, ,\ldots, sNs_N.

Output Format

One line with one integer: the number of rows among rows ABA \sim B 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 40%40\% of the testdata, A,A, B,B, C,C, D,D, N2000N \le 2000.
For all testdata, 1N5×105,1 \le N \le 5 \times 10^5, 1A,B1012,1 \le A, B \le 10^{12}, 0C,0 \le C, DN,D \le N, and C+DNC + D \le N.

Translated by ChatGPT 5