给定一个平面上的 N 个点,请你计算所有点对之间距离的平方和。
输入格式
第一行包含整数 N,表示点的个数。
接下来 N 行,每行包含两个整数 X,Y,表示一个点的横纵坐标。
注意,不保证所有点的位置各不相同。
输出格式
输出所有点对之间距离的平方和。
数据范围
1≤N≤105,
−10000≤X,Y≤10000。
输入样例:
4
1 1
-1 -1
1 -1
-1 1
输出样例:
32
x如此,y同理
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 100010;
int n;
int x[N],y[N];
LL get(int x[])
{
LL s1 = 0,s2 = 0;
for(int i=0; i<n; ++i){
s1 += x[i]*x[i];
s2 += x[i];
}
return n*s1 - s2*s2;
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i ++ )
scanf("%d%d", &x[i], &y[i]);
cout<<get(x)+get(y)<<endl;
return 0;
}