E. Points
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output
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.
Examples
input
4
1 1
-1 -1
1 -1
-1 1
output
32
思路
暴力会超时,此处需要用到一个数学转换式子也就是负的2乘以X2乘以X1就等于X2的平方加上X1的平方减去X1加上X2的和的平方 所以就转换成了
X部分的和为n*(x12+x22+x32…Xn2)-(x1+x2+x3+…Xn)^2
Y部分的和为n*(y12+y22+y32+…Yn2)-(y1+y2+y3+…Yn)^2
####### AC代码
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100010;
int main()
{
long long e=0, n,c=0,d=0,x,y;//声明要注意用longlong,记得初始化为0或者全局变量
cin>>n;
for(int i=0;i<n;i++)
{
cin>>x>>y;
e+=x*x+y*y;
c+=x;
d+=y;
}
cout<<n*e-c*c-d*d<<endl;
}