日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Numbers on the Chessboard

發(fā)布時(shí)間:2023/12/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Numbers on the Chessboard 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

You are given a chessboard of size n×nn×n. It is filled with numbers from 11 to n2n2 in the following way: the first ?n22??n22? numbers from 11 to ?n22??n22? are written in the cells with even sum of coordinates from left to right from top to bottom. The rest n2??n22?n2??n22? numbers from ?n22?+1?n22?+1 to n2n2 are written in the cells with odd sum of coordinates from left to right from top to bottom. The operation ?xy??xy? means division xx by yy rounded up.

For example, the left board on the following picture is the chessboard which is given for n=4n=4 and the right board is the chessboard which is given for n=5n=5.

You are given qq queries. The ii-th query is described as a pair xi,yixi,yi. The answer to the ii-th query is the number written in the cell xi,yixi,yi (xixi is the row, yiyi is the column). Rows and columns are numbered from 11 to nn.

Input
The first line contains two integers nn and qq (1≤n≤1091≤n≤109, 1≤q≤1051≤q≤105) — the size of the board and the number of queries.

The next qq lines contain two integers each. The ii-th line contains two integers xi,yixi,yi (1≤xi,yi≤n1≤xi,yi≤n) — description of the ii-th query.

Output
For each query from 11 to qq print the answer to this query. The answer to the ii-th query is the number written in the cell xi,yixi,yi (xixi is the row, yiyi is the column). Rows and columns are numbered from 11 to nn. Queries are numbered from 11 to qq in order of the input.

Examples
Input
4 5
1 1
4 4
4 3
3 2
2 4
Output
1
8
16
13
4
Input
5 4
2 1
4 2
3 3
3 4
Output
16
9
7
20
Note
Answers to the queries from examples are on the board in the picture from the problem statement.
這個(gè)題感覺(jué)上就是找規(guī)律,題目的方法很多,我是根據(jù)n的奇偶性,以及(x+y)的奇偶性來(lái)判斷,推斷出式子來(lái)就可以了。注意用長(zhǎng)整型。
代碼如下:

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define ll long long using namespace std;ll n; ll x,y; ll t;int main() {while(scanf("%I64d%I64d",&n,&t)!=EOF){while(t--){scanf("%I64d%I64d",&x,&y);if(n%2==0){ll num=(x-1)*(n/2)+((y+1)/2);if((x+y)%2==0)printf("%I64d\n",num);else printf("%I64d\n",num+n*n/2);}else{ll num=((x-1)*n+1+y)/2;if((x+y)%2==0)printf("%I64d\n",num);else printf("%I64d\n",num+n*n/2+1);}}}return 0; }

努力加油a啊,(o)/~

總結(jié)

以上是生活随笔為你收集整理的Numbers on the Chessboard的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。