Under construction

Still being built. Things may change

Diagonal Numbering

Interview

Time limit1sMemory limit1024 MB

Summary
Given an r by c table numbered along upper-right to lower-left diagonals, find the row and column of each of q given cell numbers.
Level

Medium4 of 10

Topics
Math, Implementation, Binary search
Solved
No attempts yet

Problem

Number the cells of a rectangular table with rr rows and cc columns, starting from the top left corner. Cells are numbered along diagonals that run from the upper right to the lower left. Cells on the same diagonal are numbered from top to bottom.

For example, a 3×53 \times 5 table is numbered as follows:

124710
3581113
69121415

qq cell numbers are given. For each number, find the cell that contains it.

Input

The first line contains three integers rr, cc, and qq (1≤r,c≤1091 \le r, c \le 10^9, 1≤q≤1001 \le q \le 100).

The second line contains qq integers 1≤n1<n2<…<nq≤r⋅c1 \le n_1 < n_2 < \ldots < n_q \le r \cdot c.

Output

Print qq lines. For each nin_i, print two integers: the row and the column of the cell that contains it. Rows are numbered from 1 from top to bottom, and columns are numbered from 1 from left to right.

Examples1

  1. Example 1

    Input
    3 5 15
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    
    Expected output
    1 1
    1 2
    2 1
    1 3
    2 2
    3 1
    1 4
    2 3
    3 2
    1 5
    2 4
    3 3
    2 5
    3 4
    3 5