找规律-CodeForces 1027B-Numbers on the Chessboard
B. Numbers on the Chessboard
题目大意:
填数问题,坐标(1开始)x+y为偶数的填 1~n*n/2 的数,其他数填在x+y为奇数的各种,给定坐标,求数值
题解:
考虑矩阵阶的奇偶
n%2==0:
x+y为偶数:num=(x-1)*(n/2)+((y+1)/2 )
x+y为奇数:num+n*n/2
n%2==1:
x+y为偶数:num=((x-1)*n+y+1)/2
x+y为奇数:num+n*n/2+1
代码:
include
include
include
include
include
include
using namespace std;
define inf 0x3f3f3f3f
define ll long long
:sync_with_stdio(false)" class="reference-link">define closeio std:
:sync_with_stdio(false)
define mem(a,b) memset(a,b,sizeof(a))
int main()
{ll n,m,i;
cin>>n>>m;
while(m--)
{
ll x,y,num;
cin>>x>>y;
if(n%2==0)
{
num=(x-1)*(n/2)+((y+1)/2);
if((x+y)%2==0)
cout<<num<<endl;
else
cout<<num+n*n/2<<endl;
}
else
{
num=((x-1)*n+y+1)/2;
if((x+y)%2==0)
cout<<num<<endl;
else
cout<<num+n*n/2+1<<endl;
}
}
return 0;
}
还没有评论,来说两句吧...