Codeforces Round #642 (Div. 3) C. Board Moves

C. Board Moves
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a board of size n×n, where n is odd (not divisible by 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) you can move the figure to cells:

(i−1,j−1);
(i−1,j);
(i−1,j+1);
(i,j−1);
(i,j+1);
(i+1,j−1);
(i+1,j);
(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. n2−1 cells should contain 0 figures and one cell should contain n2 figures).

You have to answer t independent test cases.

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

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

It is guaranteed that the sum of n over all test cases does not exceed 5⋅105 (∑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
inputCopy
3
1
5
499993
outputCopy
0
40
41664916690999888

给定一个n*n (n是奇数)的棋盘,棋盘上每个格子有妻子,
每个旗子都可以走上下左右对角8个方向
求把所有旗子移动到同一格上的最小步数,
如图,把所有旗子移动到中心点就是最小的
可以得到

  1. 中心点的移动步数是0
  2. 中心外一圈8个旗子的步数是1
  3. 中心外两圈的16个旗子步数是2
  4. 依次类推中心外n圈的xxx个旗子步数是n

//#define debug
#ifdef debug
#include <time.h>
#include "/home/majiao/mb.h"
#endif


#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define int long long int
#define QAQ (0)

using namespace std;

#ifdef debug
#define show(x...) \ do { \ cout << "\033[31;1m " << #x << " -> "; \ err(x); \ } while (0)
void err() { cout << "\033[39;0m" << endl; }
#endif

template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

int n, m, Q, K;

signed main() {
#ifdef debug
	freopen("test", "r", stdin);
	clock_t stime = clock();
#endif
	scanf("%lld ", &Q);
	while(Q--) {
		scanf("%lld ", &n);
		int sum = 0, now = 0, step = 1;
		for(int i=1; (now=3+(i-1)*2)<=n; i++, step++) {
			sum += (now-1)*step*4;
// printf("i:%lld %lld\n", i, now);
		}
		printf("%lld\n", sum);
	}


	










#ifdef debug
	clock_t etime = clock();
	printf("run time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif 
	return 0;
}


全部评论

相关推荐

头像
不愿透露姓名的神秘牛友
04-18 01:10
字节 后端 10k go
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务