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

这里写图片描述
这里写图片描述
这里写图片描述

Examples

input

4 5
1 1
4 4
4 3
3 2
2 4

output

1
8
16
13
4

input

5 4
2 1
4 2
3 3
3 4

output

16
9
7
20

题意:有一个n*n的方格,先给行(i)、列(j)相加为偶数的格子依次填上编号,再给行列相加为奇数的格子填上编号。现要求某行某列的方格中填的数是多少。
分析:看懂了题目就能做了,并不知道为什么有人会WA。注意这里要用long long。
下面贴代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
#define ll long long
using namespace std;

int main()
{
        ll n;
        int q;
        scanf("%I64d%d",&n,&q);
        ll x1=n/2+(n%2==1?1:0);
        ll x2=n/2;
        ll en=(x1+x2)*(n/2)+(n%2==1?x1:0);  //最后一个偶数格子里填的数
        ll x,y;
        while(q--)
            {
                scanf("%I64d%I64d",&x,&y);
                if((x+y)%2==0)
                {
                    ll num=(x1+x2)*((x-1)/2)+((x-1)%2==1?x1:0);
                    if(x%2==1)
                        num+=(y+1)/2;
                    else
                        num+=y/2;
                    printf("%I64d\n",num);
                }
                else
                {
                    ll num=en+(x1+x2)*((x-1)/2)+((x-1)%2==1?x2:0);
                    if(x%2==1)
                        num+=y/2;
                    else
                        num+=(y+1)/2;
                    printf("%I64d\n",num);
                }

            }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值