The essay#10452

The essay (Editorial)

Let's find a formula for solving the problem based on the number of words in a sentence.

Let be an even number. Let us consider Barış's and Aziz's sentences:

Let's compute the sum of the numbers in the "difference" array. Split the numbers in the array into two equal parts. Find the sum of the numbers from to n - 1 with a step of (this is an arithmetic progression). There are numbers in this range. Therefore, the sum of all the numbers in the "difference" array is:

2 \cdot {1 + n - 1 \over 2} \cdot {n \over 2} = {n^2 \over 2}

Let be an odd number. Let us consider Barış's and Aziz's sentences:

Let's compute the sum of the numbers in the "difference" array. Split the numbers in the array into two equal parts. Find the sum of the numbers from to n - 1 with a step of (this is an arithmetic progression). There are (n - 1) / 2 numbers in this range. Therefore, the sum of all the numbers in the "difference" array is:

2 \cdot {2 + n - 1 \over 2} \cdot {n - 1 \over 2} = {(n + 1) \cdot (n - 1) \over 2} = {n^2 - 1 \over 2}

Example

Let n = 8.

The sum of the numbers in the "difference" array is 8^2 / 2 = 32.

Let n = 7.

The sum of the numbers in the "difference" array is (7^2 - 1) / 2 = 24.

Algorithm implementation

Read the number of test cases .

scanf("%d", &tests);
while (tests--)
{

For each value of , compute and print the result.

  scanf("%d", &n);
  if (n % 2 == 0)
    res = n * n / 2;
  else
    res = (n * n - 1) / 2;
  printf("%lld\n", res);
}

Python implementation

Read the number of test cases .

tests = int(input())

for _ in range(tests):

For each value of , compute and print the result.

  n = int(input())
  if n % 2 == 0:
    res = n * n // 2
  else:
    res = (n * n - 1) // 2
  print(res)