CodeForces 76E

E. Points

time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output
You are given N points on a plane. Write a program which will find the sum of squares of distances between all pairs of points.

Input

The first line of input contains one integer number N (1 ≤ N ≤ 100 000) — the number of points. Each of the following N lines contain two integer numbers X and Y ( - 10 000 ≤ X, Y ≤ 10 000) — the coordinates of points. Two or more points may coincide.

Output

The only line of output should contain the required sum of squares of distances between all pairs of points.

Examples
input

4
1 1
-1 -1
1 -1
-1 1

output

32

思路

暴力会超时,此处需要用到一个数学转换式子也就是负的2乘以X2乘以X1就等于X2的平方加上X1的平方减去X1加上X2的和的平方 所以就转换成了
X部分的和为n*(x12+x22+x32…Xn2)-(x1+x2+x3+…Xn)^2
Y部分的和为n*(y12+y22+y32+…Yn2)-(y1+y2+y3+…Yn)^2

####### AC代码

#include<iostream>
#include<algorithm>

using namespace std;

const int N=100010;

int main()
{
	long long e=0, n,c=0,d=0,x,y;//声明要注意用longlong,记得初始化为0或者全局变量
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>x>>y;
		e+=x*x+y*y;
		c+=x;
		d+=y;
	}
	cout<<n*e-c*c-d*d<<endl;
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值