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 n×n, where n n n is odd (not divisible by 2 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) (i,j) you can move the figure to cells:

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

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

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

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

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

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

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

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

You have to answer t t t independent test cases.

1.3. Input

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

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

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

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 i 层有 8 i 8i 8i 个元素,需要移动 i i i 次才能到达矩阵中心。

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

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

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 f(2x1)=f(2(x1)1)+8×(2(x1)1)2


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

x x x 2 x − 1 2x-1 2x1结果差值
110
2388
354032
4711472
59506392
6111164648

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]);
    }
}


联系邮箱:curren_wong@163.com

Github:https://github.com/CurrenWong

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

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值