zoukankan      html  css  js  c++  java
  • Codeforces Round #642 (Div. 3) C. Board Moves

    You are given a board of size n×nn×n , where nn is odd (not divisible by 22 ). Initially, each cell of the board contains one figure.

    In one move, you can select exactly one figure presented in some cell and move it to one of the cells sharing a side or a corner with the current cell, i.e. from the cell (i,j)(i,j) you can move the figure to cells:

    • (i1,j1)(i−1,j−1) ;
    • (i1,j)(i−1,j) ;
    • (i1,j+1)(i−1,j+1) ;
    • (i,j1)(i,j−1) ;
    • (i,j+1)(i,j+1) ;
    • (i+1,j1)(i+1,j−1) ;
    • (i+1,j)(i+1,j) ;
    • (i+1,j+1)(i+1,j+1) ;

    Of course, you can not move figures to cells out of the board. It is allowed that after a move there will be several figures in one cell.

    Your task is to find the minimum number of moves needed to get all the figures into one cell (i.e. n21n2−1 cells should contain 00 figures and one cell should contain n2n2 figures).

    You have to answer tt independent test cases.

    Input

    The first line of the input contains one integer tt (1t2001≤t≤200 ) — the number of test cases. Then tt test cases follow.

    The only line of the test case contains one integer nn (1n<51051≤n<5⋅105 ) — the size of the board. It is guaranteed that nn is odd (not divisible by 22 ).

    It is guaranteed that the sum of nn over all test cases does not exceed 51055⋅105 (n5105∑n≤5⋅105 ).

    Output

    For each test case print the answer — the minimum number of moves needed to get all the figures into one cell.

    Example
    Input
    Copy
    3
    1
    5
    499993
    
    Output
    Copy
    0
    40
    41664916690999888

    找规律。可以看到正方形的每一个“圈”到中心的最小距离都是一样的(数一下就能发现规律)。然后把整个正方形分成八个小三角形+八条线或者直接按照圈来统计都行。
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            long long n,ans=0;
            long long i;
            cin>>n;
            for(i=2;i<=n/2+1;i++)
            {
                ans+=(i-2)*(i-1);
            } 
            ans*=8;
            for(i=1;i<=n/2+1;i++)
            {
                ans+=(i-1)*8;
            }      
            cout<<ans<<endl;                                                                              
        }
        return 0;
    }
  • 相关阅读:
    docker基本命令
    服务器端使用DeferredResult异步推送技术
    单链表输出倒数第k个结点值(栈实现)
    fork炸弹
    BlockingQueue实现阻塞队列
    C#工作笔记
    实现一个简单的Log框架
    (转载)在spring的bean中注入内部类
    Spring整合Hessian
    spring整合axis2(最小配置化)的示例
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12894731.html
Copyright © 2011-2022 走看看