B. Numbers on the Chessboard -codeforces1027 -csdn博客
B. Numbers on the Chessboard -codeforces1027 -csdn博客
You are given a chessboard of size n×n. It is filled with numbers from 1 to n2 in the following way: the first ?n22? numbers from 1 to ?n22? are written in the cells with even sum of coordinates from left to right from top to bottom. The rest n2??n22? numbers from ?n22?+1 to n2 are written in the cells with odd sum of coordinates from left to right from top to bottom. The operation ?xy? means division x by y rounded up.
For example, the left board on the following picture is the chessboard which is given for n=4 and the right board is the chessboard which is given for n=5.
You are given q queries. The i-th query is described as a pair xi,yi. The answer to the i-th query is the number written in the cell xi,yi (xi is the row, yi is the column). Rows and columns are numbered from 1 to n.
Input
The first line contains two integers n and q (1≤n≤109, 1≤q≤105) — the size of the board and the number of queries.
The next q lines contain two integers each. The i-th line contains two integers xi,yi (1≤xi,yi≤n) — description of the i-th query.
Output
For each query from 1 to q print the answer to this query. The answer to the i-th query is the number written in the cell xi,yi (xi is the row, yi is the column). Rows and columns are numbered from 1 to n. Queries are numbered from 1 to q in order of the input.
Examples
inputCopy
4 5
1 1
4 4
4 3
3 2
2 4
outputCopy
1
8
16
13
4
inputCopy
5 4
2 1
4 2
3 3
3 4
outputCopy
16
9
7
20
Note
Answers to the queries from examples are on the board in the picture from the problem statement.
題意:給你一個n×n的方框,在里面根據規則填數字,有q個查詢,輸出每個查詢x,y對應位置數字大小。
規則:行和列之和為偶數是,數字為前n^2/2(上限),順序從左到右,從上倒下,如果為奇數,則為n^2/2+1到n^2;題解:首先判斷行和列之和奇偶行,如果是奇數,那么加上n^2/2(如果n^2為奇數,那么要加上1)。然后就是你惡不的判斷了。我們首先可以判定如果行滿足是兩行連續時,那么一共有n個數填了進去,因此我用(a-1)/2看看滿足的有多少個滿兩行對。具體操作如下。
關于這個題的解法大家可以自己畫圖試一試,找一找為什么要這樣寫
歡迎歡迎,如果大家喜歡的話可以關注一波O(∩_∩)O哈哈~
posted @ 2018-08-19 13:51 i-Curve 閱讀(...) 評論(...) 編輯 收藏總結
以上是生活随笔為你收集整理的B. Numbers on the Chessboard -codeforces1027 -csdn博客的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTC为何死掉?
- 下一篇: 微型计算机常用接口实验,微型计算机原理及