D. Number of Parallelograms
You are given n points on a plane. All the points are distinct and no three of them lie on the same line. Find the number of parallelograms with the vertices at the given points.
Input
The first line of the input contains integer n (1 ≤ n ≤ 2000) — the number of points.
Each of the next n lines contains two integers (xi, yi) (0 ≤ xi, yi ≤ 109) — the coordinates of the i-th point.
Output
Print the only integer c — the number of parallelograms with the vertices at the given points.
Example
input
4 0 1 1 0 1 1 2 0
output
1
It's known that the diagonals of a parallelogram split each other in the middle. Let's iterate over the pairs of points a, b and consider the middle of the segment
:
. Let's calculate the value cntc for each middle. cntc is the number of segments a, b with the middlec. Easy to see that the answer is
.
-----------------------------------------------CODE--------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
vector<pair<lli,lli> > v;
map<pair<lli,lli> ,int> ma;
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
lli a,b;
cin>>a>>b;
v.push_back({a,b});
}
for(int i=0;i<n;i++)
{
lli x1=v[i].first;
lli y1=v[i].second;
for(int j=i+1;j<n;j++)
{
lli x2=v[j].first;
lli y2=v[j].second;
lli up=y2+y1;
lli dwn=x2+x1;
ma[{up,dwn}]++;
}
}
map<pair<lli,lli>,int> :: iterator it;
lli ans=0;
for(it=ma.begin();it!=ma.end();it++)
{
lli val=it->second;
ans+=(val*(val-1))/2;
}
cout<<ans<<endl;
}