找规律-CodeForces 1027B-Numbers on the Chessboard

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SZU_Crayon/article/details/82182927
  • 找规律-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<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
#define closeio std::ios::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;
}

猜你喜欢

转载自blog.csdn.net/SZU_Crayon/article/details/82182927