Sum of Subrectangle Areas
Time limit2sMemory limit512 MB
For each N, compute the total area of all axis-aligned integer subrectangles in an N by N grid.
- Level
Easy3 of 10
- Topics
- Math, Combinatorics
- Solved
- No attempts yet
Problem
Consider the grid from to .
Consider every subrectangle whose four corners have integer coordinates, that is, every with and . The area of such a rectangle is . The following pseudocode computes the sum of the areas of all subrectangles.
sum = 0
for r1 = 0 to N-1
for c1 = 0 to N-1
for r2 = r1+1 to N
for c2 = c1+1 to N
sum = sum + (r2-r1)*(c2-c1)
print(sum)
Given , compute this sum efficiently. The input contains several test cases.
Input
The first line contains the number of test cases . Each of the following lines contains a single integer for that test case. fits in a 64-bit integer with .
Output
For each test case, print the sum defined above on its own line. The sum may exceed the range of a 64-bit integer, so print it as an arbitrary precision integer.