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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

B. Numbers on the Chessboard -codeforces1027 -csdn博客

發布時間:2023/12/29 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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看看滿足的有多少個滿兩行對。具體操作如下。
    關于這個題的解法大家可以自己畫圖試一試,找一找為什么要這樣寫

#include<bits/stdc++.h> #define endl '\n' #define pb push_back #define mp make_pair #define _ ios::sync_with_stdio(false) bool SUBMIT = 1; typedef long long ll; using namespace std; const double PI = acos(-1); ll n,q; int main() {if(!SUBMIT)freopen("i.txt","r",stdin);else _;cin>>n>>q;for(ll i=0;i<q;i++){ll a,b;cin>>a>>b;ll c;if(n*n%2)c=n*n/2+1;else c=n*n/2;ll ans=0;if(a>2)ans+=n*((a-1)/2);a-=(a-1)/2*2;if(a>1)ans+=n/2;if((a+b)%2){//if(a>1&&n%2)ans++;//此時不用這句話,因為奇數的話加在了前n^2/2內ans+=(b+1)/2;ans+=c;}else{if(a>1&&n%2)ans++;//必須有,n為奇數且前半部分的話,每對第一行還要多一個數ans+=(b+1)/2;}cout<<ans<<endl;}return 0; }

歡迎歡迎,如果大家喜歡的話可以關注一波O(∩_∩)O哈哈~

posted @ 2018-08-19 13:51 i-Curve 閱讀(...) 評論(...) 編輯 收藏

總結

以上是生活随笔為你收集整理的B. Numbers on the Chessboard -codeforces1027 -csdn博客的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。