C. Board Moves

Problem - C - Codeforces

You are given a board of size n×nn×n, where nn is odd (not divisible by 22). 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. n2−1n2−1 cells should contain 00 figures and one cell should contain n2n2 figures).

You have to answer tt independent test cases.

Input

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

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

It is guaranteed that the sum of nn over all test cases does not exceed 5⋅1055⋅105 (∑n≤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

input

Copy

3
1
5
499993

output

Copy

0
40
41664916690999888

发现规律了;

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll n,num=0;
        cin>>n;
        for(ll i=1,k=8;i<=n/2;i++,k+=8)//i一定开long long
        {
        	num+=i*k;
        }
        cout<<num<<endl;
    }
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import heapq import copy # 定义状态类 class State: def __init__(self, board, moves=0, parent=None, last_move=None): self.board = board self.moves = moves self.parent = parent self.last_move = last_move def __lt__(self, other): return self.moves < other.moves def __eq__(self, other): return self.board == other.board # 定义转移函数 def move(state, direction): new_board = copy.deepcopy(state.board) for i in range(len(new_board)): if 0 in new_board[i]: j = new_board[i].index(0) break if direction == "up": if i == 0: return None else: new_board[i][j], new_board[i-1][j] = new_board[i-1][j], new_board[i][j] elif direction == "down": if i == len(new_board)-1: return None else: new_board[i][j], new_board[i+1][j] = new_board[i+1][j], new_board[i][j] elif direction == "left": if j == 0: return None else: new_board[i][j], new_board[i][j-1] = new_board[i][j-1], new_board[i][j] elif direction == "right": if j == len(new_board)-1: return None else: new_board[i][j], new_board[i][j+1] = new_board[i][j+1], new_board[i][j] return State(new_board, state.moves+1, state, direction) # 定义A*算法 def astar(start, goal): heap = [] closed = set() heapq.heappush(heap, start) while heap: state = heapq.heappop(heap) if state.board == goal: path = [] while state.parent: path.append(state) state = state.parent path.append(state) return path[::-1] closed.add(state) for direction in ["up", "down", "left", "right"]: child = move(state, direction) if child is None: continue if child in closed: continue if child not in heap: heapq.heappush(heap, child) else: for i, (p, c) in enumerate(heap): if c == child and p.moves > child.moves: heap[i] = (child, child) heapq.heapify(heap) # 测试 start_board = [[1, 2, 3], [4, 5, 6], [7, 8, 0]] goal_board = [[2, 3, 6], [1, 5, 8], [4, 7, 0]] start_state = State(start_board) goal_state = State(goal_board) path = astar(start_state, goal_board) for state in path: print(state.board)
03-27

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值