数据范围
1≤N≤105,
−10000≤X,Y≤10000。
输入样例:
4
1 1
-1 -1
1 -1
-1 1
输出样例:
32
将式子拆开,化简
可以得到
答案等于
∑
i
=
1
n
n
∗
(
x
i
∗
x
i
+
y
i
∗
y
i
)
−
(
∑
x
i
)
2
−
(
∑
y
i
)
2
\sum_{i = 1}^{n} n*(x_i*x_i+y_i*y_i) - (\sum x_i)^2-(\sum y_i)^2
i=1∑nn∗(xi∗xi+yi∗yi)−(∑xi)2−(∑yi)2
AC代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 2e5 + 3;
int main(){
int n;
scanf("%d", &n);
ll ans = 0;
ll sumx, sumy;
sumx = sumy = 0;
for(int i = 1; i <= n; ++ i){
ll x, y;
scanf("%lld %lld", &x, &y);
ans += n*x*x + n*y*y;
sumx += x;
sumy += y;
}
ans -= sumx*sumx + sumy*sumy;
if(n == 1) ans = 0;
printf("%lld\n", ans);
}