#K54072. Find Number in Alternating Pattern Canvas

    ID: 29672 Type: Default 1000ms 256MiB

Find Number in Alternating Pattern Canvas

Find Number in Alternating Pattern Canvas

You are given a canvas of size n × m filled with consecutive positive integers in an alternating order. In particular, the filling order follows these rules:

  • For odd-numbered rows (1st, 3rd, ...), the canvas is filled from left to right.
  • For even-numbered rows (2nd, 4th, ...), the canvas is filled from right to left.

For example, a 3 × 3 canvas is filled as follows:

1 2 3
6 5 4
7 8 9

Your task is to determine the number located at the position (x, y) on the canvas.

The calculation can be described by the formulas below:

  • If \(x\) is odd: \(\text{number} = (x - 1) \times m + y\).
  • If \(x\) is even: \(\text{number} = x \times m - y + 1\).

Note that the rows and columns are 1-indexed.

inputFormat

The input is provided via standard input and consists of four space-separated integers: (n), (m), (x), and (y). Here, (n) is the number of rows, (m) is the number of columns, (x) is the row index, and (y) is the column index (1-indexed).

outputFormat

Output the number at the ((x, y)) position on the canvas according to the given alternating pattern. The output should be printed to standard output.## sample

3 3 2 2
5