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 , where is odd (not divisible by ). 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 you can move the figure to cells:
-
;
-
;
-
;
-
;
-
;
-
;
-
;
-
;
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. cells should contain figures and one cell should contain figures).
You have to answer independent test cases.
1.3. Input#
The first line of the input contains one integer () — the number of test cases. Then test cases follow.
The only line of the test case contains one integer () — the size of the board. It is guaranteed that is odd (not divisible by ).
It is guaranteed that the sum of over all test cases does not exceed ().
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. 解读#
通过观察建立递推方程,从矩阵中心向外,第 层有 个元素,需要移动 次才能到达矩阵中心。
将 换为矩阵维度,。
| | | 结果 | 差值 | | :---: | :----: | :---: | :---: | | 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]);
}
}
联系邮箱:curren_wong@163.com
Github:https://github.com/CurrenWong
欢迎转载/Star/Fork,有问题欢迎通过邮箱交流。