P2552 [AHOI2001] Group Gymnastics Formation

Description

There are $n$ group gymnastics members labeled $1 \sim n$. Their formation (either continuous formation or "Meihuazhuang" formation) can be arranged by rows or by columns. When arranged by rows (or columns), each row (or column) has $r$ positions. Example 1: $n=16$, $r=6$, continuous formation arranged by rows. |1|2|3|4|5|6| |:-:|:-:|:-:|-|-|-| |**7**|**8**|**9**|**10**|**11**|**12**| |**13**|**14**|**15**|**16**| Example 2: $n=16$, $r=3$, continuous formation arranged by columns. | 1 | 4 | 7 | 10 | 13 |16| | :-: | :-: | :-: | :-: | :-: |- | |**2**|**5**|**8**|**11**|**14**| |**3**|**6**|**9**|**12**|**15**| Example 3: $n=16$, $r=7$, "Meihuazhuang" formation arranged by rows. | 1 | | 2 | | 3 | | 4 | | :-: | :-: | :-: | :-: | :-: | :-: | :-: | | |**5** | |**6** | |**7** | | |**8** | | **9**| |**10**| |**11**| | |**12**| |**13**| |**14**| | |**15**| |**16**| | | | | Example 4: $n=16$, $r=5$, "Meihuazhuang" formation arranged by columns. | 1 | | 6 | | 11 | |16 | | :-: | :-: | :-: | :-: | :-: | :-: |:-:| | |**4**| |**9** | |**14**| | |**2**| |**7**| |**12**| | | | |**5**| |**10**| |**15**| | |**3**| |**8**| |**13**| | | Please write a program to output the row index and column index of the gymnast labeled $m$.

Input Format

This problem contains multiple testcases. The first line has a single natural number $k$, indicating that the following $k$ lines each describe one testcase. Each of the next $k$ lines contains five integers, denoting respectively: the number of people in the team, the formation code ($1$ means continuous formation, and $2$ means "Meihuazhuang" formation), the arrangement method ($1$ means arranged by rows, and $2$ means arranged by columns), the number of positions per row (or column), and the label of the gymnast being queried.

Output Format

Output $2k$ integers. The $i$-th pair of integers denotes the row index and column index for the $i$-th query (no newline).

Explanation/Hint

Translated by ChatGPT 5