codeforce 400 C

来源:互联网 发布:宁波php招聘 编辑:程序博客网 时间:2024/04/30 19:24
C. Inna and Huge Candy Matrix
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to n from top to bottom and the columns — from 1 to m, from left to right. We'll represent the cell on the intersection of the i-th row and j-th column as (i, j). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix hasp candies: the k-th candy is at cell (xk, yk).

The time moved closer to dinner and Inna was already going to eat p of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix x times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix y times. And then he rotated the matrix z times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like.

Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made!

Input

The first line of the input contains fix integers nmxyzp (1 ≤ n, m ≤ 109; 0 ≤ x, y, z ≤ 109; 1 ≤ p ≤ 105).

Each of the following p lines contains two integers xkyk (1 ≤ xk ≤ n; 1 ≤ yk ≤ m) — the initial coordinates of the k-th candy. Two candies can lie on the same cell.

Output

For each of the p candies, print on a single line its space-separated new coordinates.

Sample test(s)
input
3 3 3 1 1 91 11 21 32 12 22 33 13 23 3
output
1 31 21 12 32 22 13 33 23 1

给出一个n*m的矩阵,其中有p个格子里边有糖果,知道这p个格子现在的位置,现在要将这个n*m的矩阵顺时针旋转x次,水平翻转y次,逆时针旋转z次,最后输出原来的p个有糖果的格子现在的坐标

可以找出规律,类似于二维数组旋转
且X=X%4,Y=Y%2,Z=Z%4.
故而使用穷举法即可AC

翻转规律:

顺时针旋转一次(x,y)--->(y,n+1-x)

顺时针旋转二次(x,y)--->(n+1-x,m+1-y)

顺时针旋转三次(x,y)--->(m+1-y,x)

顺时针旋转四次(x,y)--->(x,y)

 

水平翻转一次(x,y)--->(x,m+1-y)

水平翻转一次(x,y)--->(x,y)

 

逆时针旋转一次(x,y)--->(m+1-y,x)

逆时针旋转二次(x,y)--->(n+1-x,m+1-y)

逆时针旋转三次(x,y)--->(y,n+1-x)

逆时针旋转四次(x,y)--->(x,y)



0 0
原创粉丝点击