题目链接
https://codeforces.com/problemset/problem/76/E
解题思路
数学题,降维应该都能想到
由题意可知: 答案= (x1-x2)^2+(y1-y2)^2 (x1-x3)^2+(y1-y3)^2 (x2-x3)^2+(y2-y3)^2 (x1-x4)^2+(y1-y4)^2 (x2-x4)^2+(y2-y4)^2 (x3-x4)^2+(y3-y4)^2 ....... ....... (x1-xn)^2+(y1-yn)^2 (x2-xn)^2+(y2-yn)^2 .....(X(n-1)-Xn)^2+(Y(n-1)-Yn)^2 先看X部分: x1^2-2*x1*x2+x2^2 x1^2-2*x1*x3+x3^2 x2^2-2*x2*x3+x3^2 x1^2-2*x1*x4+x4^2 x2^2-2*x2*x4+x4^2 x3^2-2*x3*x4+x4^2 ..... ..... x1^2-2*x1*xn+xn^2 x2^2-2*x2*xn+xn^2 ........ X(n-1)^2-2*X(n-1)*Xn+Xn^2 仔细观察可发现 所有x^2的和 (n-1)*(x1^2+x2^2+x3^2+....Xn^2) 其它项的和为 -x1*(x2+x3+x4+...Xn)-x2*(x1+x3+x4+...Xn)-x3*(x1+x2+x4+..Xn)-....Xn*(x1+x2+x3+...X(n-1)) 可得 n*(x1^2+x2^2+x3^2+....Xn^2)-(x1+x2+x3+...Xn)^2 =(n-1)*(x1^2+x2^2+x3^2+....Xn^2)-x1*(x2+x3+x4+...Xn)-...Xn*(x1+x2+x3+...X(n-1)) 所以 X部分的和为n*(x1^2+x2^2+x3^2+....Xn^2)-(x1+x2+x3+...Xn)^2 同理 Y部分的和为n*(y1^2+y2^2+y3^2+....Yn^2)-(y1+y2+y3+...Yn)^2
代码
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5+10; ll sum, sumx, sumy, ans, x, y; int n; int main() { scanf("%d", &n); for(int i = 1;i <= n;i ++) { scanf("%lld %lld", &x, &y); ans += x*x + y*y; sumx += x; sumy += y; } printf("%lld\n", n*ans - sumx*sumx - sumy*sumy); return 0; }