loj#P6790. 「ICPC World Finals 2020」扑克牌学

「ICPC World Finals 2020」扑克牌学

Description

The Great Cardoni, Master Prestidigitator, has a deck of 2121 numbered cards which he uses in a trick as follows:

A spectator secretly selects a number between 11 and 2121, inclusive, after which Cardoni deals the 2121 cards, face-up, row by row in order from 11 to 2121, into a 33-column grid. The spectator then indicates which of the three columns contains the selected card, at which point the magician picks up the cards by columns, picking up the specified column second (the order of collecting the other two columns is unimportant). Cards are collected face up, beginning with the top card in each column and placing each succeeding card immediately beneath the previously collected card. The cards are then redealt by rows into a 33-column grid, starting from the top of the face-up deck. The process is repeated two more times; each time, the column indicated by the spectator is the second column picked up by the magician. After three such iterations, Cardoni announces, "I have penetrated to the heart of your mind; your card lies at the heart of this display." And it's true—the selected card is located at the "heart" of the array (row four, column two). Moreover, the selected card will always remain in this stable location for any further iterations of the column indication and card redealing process.

The process always works, no matter the number selected, provided that the column containing the secret number is the second column to be picked up and redealt.

Cardoni would like to expand his trick to use different numbers of cards, rows, and columns, and to experiment with different orderings of picking up the columns after the spectator indicates a column. However, it is not a trivial problem. For instance, when using 2424 cards in 88 rows and 33 columns, and always picking up the indicated column as the second one to redeal, the number 55 eventually ends up in stable location row 44, column 33, while the number 2020 ends up in stable location row 55, column 11. Also, neither location is one of the two "heart" positions in column 22 of rows 44 or 55. Moreover, Cardoni is uncertain of how many iterations of the "indicate column and redeal" process are needed before a selected card reaches a stable location.

Given the number of rows and columns of cards, help Cardoni set up his trick in such a way that there is a unique stable location that is as close to the center as possible.

Input

The input consists of a single line with two integers rr and cc (2r,c106)(2 \le r, c \le 10^6), the number of rows and columns used in the trick. The cards are numbered from 11 to rcr \cdot c and are initially dealt row by row in increasing order.

Output

Output a line containing four integers p,i,jp, i, j, and ss, where:

  • the column indicated by the spectator should be picked up as the pthp^\text{th} column,
  • using this value of pp causes all cards to eventually end up in the stable location at row ii column jj, and
  • ss is the maximum number of iterations required for any card to reach the stable location.

The value of pp should be chosen so that the stable location (i,j)(i, j) is as close as possible to any of the one, two or four central positions in the grid, where the distance between locations (i,j)(i, j) and (i,j)(i', j') is ii+jj|i − i'| + |j − j'|. If more than one value of pp results in the same minimum distance, choose the smallest such pp.

7 3

2 4 2 3

8 3

1 1 1 3