CF1027B Numbers on the Chessboard (#数学)
題目描述
You are given a chessboard of size?n \times nn×n?. It is filled with numbers from?11?to?n^2n2?in the following way: the first?\lceil \frac{n^2}{2} \rceil?2n2???numbers from?11?to?\lceil \frac{n^2}{2} \rceil?2n2???are written in the cells with even sum of coordinates from left to right from top to bottom. The rest?n^2 - \lceil \frac{n^2}{2} \rceiln2??2n2???numbers from?\lceil \frac{n^2}{2} \rceil + 1?2n2??+1?to?n^2n2?are written in the cells with odd sum of coordinates from left to right from top to bottom. The operation?\lceil\frac{x}{y}\rceil?yx???means division?xx?by?yyrounded 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?x_i, y_ixi?,yi??. The answer to the?ii?-th query is the number written in the cell?x_i, y_ixi?,yi??(?x_ixi??is the row,?y_iyi??is the column). Rows and columns are numbered from?11?to?nn?.
輸入輸出格式
輸入格式:
The first line contains two integers?nn?and?qq?(?1 \le n \le 10^91≤n≤109?,?1 \le q \le 10^51≤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?x_i, y_ixi?,yi??(?1 \le x_i, y_i \le n1≤xi?,yi?≤n?) — description of the?ii?-th query.
輸出格式:
For eachuery from?11?to?qq?print the answer to this query. The answer to the?ii?-th query is the number written in the cell?x_i, y_ixi?,yi??(?x_ixi??is the row,?y_iyi??is the column). Rows and columns are numbered from?11?to?nn?. Queries are numbered from?1?to?q?in order of the input.
輸入輸出樣例
輸入樣例#1
4 5 1 1 4 4 4 3 3 2 2 4輸出樣例#1
1 8 16 13 4輸入樣例#2
5 4 2 1 4 2 3 3 3 4輸出樣例#2
16 9 7 20說明
Answers to the queries from examples are on the board in the picture from the problem statement.
思路
非常難搞的數學題......CF B題都那么難的么?
大意是按題目中的圖填數,輸入x,y代表圖中的坐標,輸出(x,y)的數值。
第一輪填數的橫縱坐標和為偶數,第二輪填數的橫縱坐標和為奇數。找規律可得數值與坐標的關系是(x-1)*n+(y+1)。
第二輪只要加上(n*n)就好了。
答案最后別忘了除以2,因為第一輪和第二輪我們是分情況討論的。
#include <stdio.h> #include <iostream> using namespace std; long long int n,t,s; signed main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cin>>n>>t;while(t--){int x,y;s=0;cin>>x>>y;s=(x-1)*n+y+1;if((x+y)%2){s+=n*n;}cout<<s/2<<endl;}return 0; }?
總結
以上是生活随笔為你收集整理的CF1027B Numbers on the Chessboard (#数学)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安森美半导体增强CCD图像传感器的近红外
- 下一篇: PE文件 反汇编