Points (Codeforces 76E)
Tags: Implement, math

You are given N points on a plane. Write a program which will find the sum of squares of distances between all pairs of points.

Input: The first line of input contains one integer number N (1 ≤ N ≤ 100 000) — the number of points. Each of the following N lines contain two integer numbers X and Y ( - 10 000 ≤ X, Y ≤ 10 000) — the coordinates of points. Two or more points may coincide.

Output: The only line of output should contain the required sum of squares of distances between all pairs of points.


Sample Inputs:
4
1 1
-1 -1
1 -1
-1 1

Sample Outputs:
32

Solution (Python):
import sys
n = int(input())
ssq = 0
sqs0 = 0
sqs1 = 0
for s in sys.stdin.readlines():
    (a, b) = s.split()
    (a, b) = (int(a), int(b))
    ssq += a * a + b * b
    sqs0 += a
    sqs1 += b
print(n * ssq - sqs0 ** 2 - sqs1 ** 2)

Solution (Python):
n = int(input())
cur = 0
count = 0
count1 = 0
for i in range(n):
    (x, y) = map(int, input().split())
    cur += n * x * x
    cur += n * y * y
    count += x
    count1 += y
cur = cur - count * count - count1 * count1
print(cur)

Solution (Python):
import sys
n = int(input())
ssq = 0
sqs0 = 0
sqs1 = 0
for _ in range(n):
    (a, b) = sys.stdin.readline().split()
    (a, b) = (int(a), int(b))
    ssq += a * a + b * b
    sqs0 += a
    sqs1 += b
print(n * ssq - sqs0 ** 2 - sqs1 ** 2)