CodeForces - 76E
题目: https://codeforces.com/problemset/problem/76/E
暴力会超时,需要用一点数学知识,把对应的(xi - xj)^2 + (yi - yj)^2 拆分一下,具体如上,然后分别合并X和Y,不合并是N^2复杂度,合并后O(n),不会超时,推理如上
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <stack>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double PI = acos(-1);
const int N = 100010;
ll x, y, s, ans, res, t, tt;
int n;
int main()
{
scanf("%d", &n);
for(int i = 0;i < n;i++)
{
scanf("%lld%lld", &x, &y);
ans += x * x;
res += y * y;
t += x;
tt += y;
}
printf("%lld\n", n * (ans + res) - t * t - tt * tt);
return 0;
}