题目描述
Quido plans to send a New Year greeting to his friend Hugo. He has recently acquired access to an advanced high-precision plotter and he is planning to print the greeting card on the plotter.
Here’s how the plotter operates. In step one, the plotter plots an intricate pattern of n dots on the paper. In step two, the picture in the greeting emerges when the plotter connects by a straight segment each pair of dots that are exactly 2 018 length units apart.
The plotter uses a special holographic ink, which has a limited supply.Quido wants to know the number of all plotted segments in the picture to be sure that there is enough ink to complete the job.
输入
The first line of input contains a positive integer n specifying the number of plotted points. The following n lines each contain a pair of space-separated integer coordinates indicating one plotted point. Each coordinate is non-negative and less than 231. There are at most 105 points, all of them are distinct.
In this problem, all coordinates and distances are expressed in plotter length units, the length of the unit in the x-direction and in the y-direction is the same.
输出
The output contains a single integer equal to the number of pairs of points which are exactly 2018 length units apart.
样例输入
4
20180000 20180000
20180000 20182018
20182018 20180000
20182018 20182018
样例输出
4
思路
两点之间距离为2018时,这时|x1-x2|与|y1-y2|必定只会在(0,2018)和(1118,1680)这两对点集中出现,所以可以通过这个原理来实现算法
代码实现
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const int N=5e5+5;
int n;
map<P,int> m;
ll sum;
int add[12][2]={{2018,0},{-2018,0},{0,2018},{0,-2018},{1118,1680},{-1118,1680},{1118,-1680},{-1118,-1680},{1680,1118},{-1680,1118},{1680,-1118},{-1680,-1118}};
int main()
{
scanf("%d",&n);
while(n--)
{
ll x,y;
cin>>x>>y;
sum+=m[P(x,y)];
for(int i=0;i<12;i++) m[P(x+add[i][0],y+add[i][1])]++;
}
cout<<sum<<endl;
return 0;
}