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

客官°小女子只卖身不卖艺 2022-05-15 11:59 267阅读 0赞
  • 找规律-CodeForces 1027B-Numbers on the Chessboard


  • 题目链接:

B. Numbers on the Chessboard

  • 思路:

题目大意:

填数问题,坐标(1开始)x+y为偶数的填 1~n*n/2 的数,其他数填在x+y为奇数的各种,给定坐标,求数值

24b647c14fca3cb512e5c35d990ae8cc535ec585.png

题解:

考虑矩阵阶的奇偶

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::ios::sync_with_stdio(false)

    define mem(a,b) memset(a,b,sizeof(a))

    int main()
    {

    1. ll n,m,i;
    2. cin>>n>>m;
    3. while(m--)
    4. {
    5. ll x,y,num;
    6. cin>>x>>y;
    7. if(n%2==0)
    8. {
    9. num=(x-1)*(n/2)+((y+1)/2);
    10. if((x+y)%2==0)
    11. cout<<num<<endl;
    12. else
    13. cout<<num+n*n/2<<endl;
    14. }
    15. else
    16. {
    17. num=((x-1)*n+y+1)/2;
    18. if((x+y)%2==0)
    19. cout<<num<<endl;
    20. else
    21. cout<<num+n*n/2+1<<endl;
    22. }
    23. }
    24. return 0;

    }

发表评论

表情:
评论列表 (有 0 条评论,267人围观)

还没有评论,来说两句吧...

相关阅读