#P7901. Minimum Visits to a Special Point in a Grid

    ID: 21086 Type: Default 1000ms 256MiB

Minimum Visits to a Special Point in a Grid

Minimum Visits to a Special Point in a Grid

Consider a 2n × 2n grid. A special point, named yanZhuoდ, is located at coordinates \((x,y)\) in the grid. A player, lhm, is allowed to choose any starting point in the grid and then make a sequence of moves covering exactly k points (including the starting point). The moves must obey the following rules:

  • Each move is to an adjacent cell (up, down, left, or right), and moves cannot go outside the grid.
  • The moves are divided into rounds. In each complete round, the player must visit every cell in the grid exactly once (i.e. the round forms a Hamiltonian path over all cells). After visiting every cell, that round is considered finished.
  • If the total number of moves k is not a multiple of the total number of cells \(4n^2\), then the final (partial) round consists of the first \(r = k \bmod 4n^2\) moves of a valid Hamiltonian path; it may not cover every cell.

Note that, because lhm is free to choose the starting point and plan the route, his objective is to minimize the number of times he passes through the special point \(yanZhuoდ\). In each complete round a Hamiltonian path is used, so \(yanZhuoდ\) is visited exactly once. In the final (partial) round, if any, lhm can always avoid \(yanZhuoდ\) if possible. Therefore, the minimum number of times \(yanZhuoდ\) is visited is exactly equal to the number of complete rounds, which is \(\left\lfloor \frac{k}{4n^2} \right\rfloor\).

Task: Given integers \(n\), \(k\), \(x\) and \(y\), output the minimum number of times lhm is forced to pass through the special point \(yanZhuoდ\).

Note: You may assume that lhm can always plan a route to delay visiting the special cell in the partial round if it is possible to avoid it.

inputFormat

The input consists of a single line with four space-separated integers:

  1. \(n\) — half the dimension parameter of the grid (the grid is \(2n\times2n\)).
  2. \(k\) — the total number of points that will be visited in the sequence.
  3. \(x\) and \(y\) — the coordinates of the special point (yanZhuoდ) on the grid.

You may assume that the coordinates \(x\) and \(y\) are valid (i.e. 1 ≤ x, y ≤ 2n).

outputFormat

Output a single integer: the minimum number of times that the special point \(yanZhuoდ\) is visited.

Explanation: Since each complete round visits all \(4n^2\) cells exactly once, the answer is \(\left\lfloor \frac{k}{4n^2} \right\rfloor\).

sample

1 3 1 1
0