CodeForces 1353 C. Board Moves 动态规划

1. 题目描述

1.1. Limit

Time Limit: 1 second

Memory Limit: 256 megabytes

1.2. Problem Description

You are given a board of size n × n n \times n , where n n is odd (not divisible by 2 2 ). 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:

  • ( i 1 , j 1 ) (i - 1, j - 1) ;

  • ( i 1 , j ) (i - 1, j) ;

  • ( i 1 , j + 1 ) (i - 1, j + 1) ;

  • ( i , j 1 ) (i, j - 1) ;

  • ( i , j + 1 ) (i, j + 1) ;

  • ( i + 1 , j 1 ) (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. n 2 1 n^2-1 cells should contain 0 0 figures and one cell should contain n 2 n^2 figures).

You have to answer t t independent test cases.

1.3. Input

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

The only line of the test case contains one integer n n ( 1 n < 5 1 0 5 1 \le n < 5 \cdot 10^5 ) — the size of the board. It is guaranteed that n n is odd (not divisible by 2 2 ).

It is guaranteed that the sum of n n over all test cases does not exceed 5 1 0 5 5 \cdot 10^5 ( n 5 1 0 5 \sum n \le 5 \cdot 10^5 ).

1.4. Onput

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

1.5. Sample Input

3
1
5
499993

1.6. Sample Onput

0
40
41664916690999888

1.7. Source

CodeForces 1353 C. Board Moves


2. 解读

通过观察建立递推方程,从矩阵中心向外,第 i i 层有 8 i 8i 个元素,需要移动 i i 次才能到达矩阵中心。

f ( i ) = f ( i 1 ) + 8 × ( i 1 ) 2 f(i) = f(i-1) + 8 \times (i-1)^2

i i 换为矩阵维度 x x i = 2 x 1 i = 2x-1

f ( 2 x 1 ) = f ( 2 ( x 1 ) 1 ) + 8 × ( 2 ( x 1 ) 1 ) 2 f(2x-1) = f(2(x-1)-1) + 8 \times (2(x-1)-1)^2


Table 1.样例数据表 \text{Table 1.样例数据表}

x x 2 x 1 2x-1 结果 差值
1 1 0
2 3 8 8
3 5 40 32
4 7 114 72
5 9 506 392
6 11 1164 648

3. 代码

#include <algorithm>
#include <iostream>
#include <string.h>
const long long num = 5 * 1e5 + 1;
using namespace std;

long long list[num];

void calculate(long long n)
{
    list[1] = 0;
    // 递推方程
    // 这里的 i 一定要是 long long ,不然会溢出,猜测是(i - 1) * (i - 1)用了i的类型进行存储
    for (long long i = 2; i < n; i++) {
        list[i] = list[i - 1] + 8 * (i - 1) * (i - 1);
    }
}

int main()
{
    // test case
    long long t;
    long long n;
    // 计算
    calculate((num + 1) / 2);
    // test case
    scanf("%lld", &t);
    // for each test case
    while (t--) {
        // 输入
        scanf("%lld", &n);
        // 输出
        printf("%lld\n", list[(n + 1) / 2]);
    }
}


联系邮箱:[email protected]

Github:https://github.com/CurrenWong

欢迎转载/Star/Fork,有问题欢迎通过邮箱交流。

猜你喜欢

转载自blog.csdn.net/qq_41729780/article/details/106185063
今日推荐