CodeForces - 76E Points
题意:
给你n个点的坐标,求所有一对点之间的距离的平方和
n<=100000
题解:
直接暴力n2肯定不行,我们把这个的式子列出来:
代码:
#include<bits/stdc++.h>
#define debug(a,b) printf("%s = %d\n",a,b);
typedef long long ll;
using namespace std;inline int read(){int s=0,w=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//s=(s<<3)+(s<<1)+(ch^48);return s*w;
}
const int maxn=1e5+9;
struct node{ll x,y;
}a[maxn];
ll dis(ll x,ll y,ll a,ll b){return (x-a)*(x-a)+(y-b)*(y-b);
}
int sum1[maxn],sum2[maxn];
int main()
{int n;cin>>n;ll sum=0;for(int i=1;i<=n;i++){cin>>a[i].x>>a[i].y;sum1[i]=sum1[i-1]+a[i].x;sum2[i]=sum2[i-1]+a[i].y;}for(int i=1;i<=n;i++){sum+=(n-1)*a[i].x*a[i].x-2*a[i].x*(sum1[n]-sum1[i]);sum+=(n-1)*a[i].y*a[i].y-2*a[i].y*(sum2[n]-sum2[i]);}cout<<sum<<endl;}