文章目錄
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]);
}
}
聯繫郵箱:[email protected]
Github:https://github.com/CurrenWong
歡迎轉載/Star/Fork,有問題歡迎通過郵箱交流。