PhaseRepair

Time limit: 0.2s Memory limit: 256MB Input: Output:

Task

You oversee NN Internet-of-Things devices arranged on a ring consisting of MM residues. Device ii currently shows an integer phase AiA_i with 0Ai<M0 \leq A_i \lt M. Whenever you broadcast the instruction bump residue r\text{bump residue } r, all devices whose phase is exactly rr simultaneously advance to (r+1) mod M(r + 1)\ \text{mod}\ M. You may choose your future broadcasts after observing the new phases. Your goal is to make every device show the same phase using as few broadcasts as possible.

Input data

The input file consists of:

  • a line containing integers N,MN, M.
  • a line containing the NN integers A0,,AN1A_0, \dots , A_{N−1}.

Output data

The output file must contain 22 lines:

  • a line containing integer BB: the minimum number of broadcasts required to synchronize all devices.
  • a line containing BB integers R0,,RB1R_0, \dots , R_{B−1} describing an optimal sequence of broadcasts.

Constraints and clarifications

  • 1N200 0001 \leq N \leq 200\ 000.
  • 1M200 0001 \leq M \leq 200\ 000.
  • 0Ai<M0 \leq A_i \lt M for every ii.
  • Note: any optimal sequence is accepted as long as the broadcasts are printed in the order they are performed.
# Score Constraints
1 0 Examples
2 10 Either M=1M = 1 or D=1D = 1.
3 15 N,M1000N, M \leq 1000.
4 25 D20D \leq 20.
5 20 Either every residue in [0,M1][0, M − 1] appears or all occupied residues form one contiguous arc.
6 30 No additional limitations.

Example 1

stdin

5 2
1 0 1 0 0

stdout

1
0

Explanation

In the first sample case, a single broadcast of residue 00 synchronizes all devices, and broadcasting residue 11 would also work.

Example 2

stdin

5 6
1 0 3 5 1

stdout

4
3 4 5 0

Explanation

In the second sample case, the broadcasts 33, 44, 55, 00 sweep through all occupied residues and align every device at phase 11.

Example 3

stdin

4 4
3 3 3 3

stdout

0

Explanation

In the third sample case, every device already shows the same phase, so no broadcast is needed.

Log in or sign up to be able to send submissions!