Educational Codeforces Round 49 (Rated for Div. 2) B. Numbers on the Chessboard

 http://codeforces.com/contest/1027/problem/B

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,q;
    cin>>n>>q;
    while(q--)
    {
        long long x,y;
        scanf("%I64d %I64d",&x,&y);
        if(n%2==0)
        {

            long long tmp=(x-1)*(n/2)+(y-1)/2+1;
            if((x+y)%2==0)
                printf("%I64d\n",tmp);
            else
                printf("%I64d\n",n*n/2+tmp);

        }
        else
        {
            if((x+y)%2==0)
            {
                long long tmp=(x-1)/2*n+(y-1)/2+1;
                if(x%2==0)
                {
                    printf("%I64d\n",tmp+n/2+1);

                }
                else
                {
                     printf("%I64d\n",tmp);

                }
            }
            else
            {
                long long tmp=(x-1)/2*n+(y-1)/2+1+n*n/2+1;
                if(x%2==0)
                {
                    printf("%I64d\n",n/2+tmp);

                }
                else
                {
                     printf("%I64d\n",tmp);

                }

            }

        }
    }
    return 0;
}

分多种情况讨论,推公式就行。

posted @ 2018-08-19 20:59  eason99  阅读(65)  评论(0编辑  收藏  举报