POJ 1971 Parallelogram Counting

来源:互联网 发布:淘宝确认收款是几天 编辑:程序博客网 时间:2024/04/29 20:39

题目:

Description

There are n distinct points in the plane, given by their integer coordinates. Find the number of parallelograms whose vertices lie on these points. In other words, find the number of 4-element subsets of these points that can be written as {A, B, C, D} such that AB || CD, and BC || AD. No four points are in a straight line.

Input

The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases. It is followed by the input data for each test case. 
The first line of each test case contains an integer n (1 <= n <= 1000). Each of the next n lines, contains 2 space-separated integers x and y (the coordinates of a point) with magnitude (absolute value) of no more than 1000000000. 

Output

Output should contain t lines. 
Line i contains an integer showing the number of the parallelograms as described above for test case i. 

Sample Input

260 02 04 01 13 15 17-2 -18 95 71 14 82 09 8

Sample Output

56

这个题目,首先是平行四边形的性质。

A+D=B+C,这里的ABCD可以理解为向量,或者复数。

还可以理解为,关于坐标的线性函数,比如x+y,3x+5y这种。

本题中,为了用不同的整数表示不同的点,我用的是2000000002x+y(因为x和y有正负)

这样的数,大小约为-4*10^18到4*10^18,sum的大小约为-8*10^18到8*10^18

在sum的初始化的时候,我设置为0x7f7f7f7f7f7f7f7f,约为9*10^18,大于sum最大值。

代码:

#include<iostream>#include<algorithm>#include<string.h>using namespace std;long long h=1000000000;long long list[1001];long long sum[1000000];long long x, y;int main(){int t, n;cin >> t;while (t--){cin >> n;for (int i = 0; i < n; i++){cin >> x >> y;list[i] = x*(h * 2 + 2) + y;//点到整数的哈希}memset(sum, 0x80, sizeof(sum));for (int i = 0; i < n; i++)for (int j = i+1; j < n; j++)sum[i*n + j] = list[i] + list[j];//数组选2个数求和的哈希sort(sum, sum + n*n);int ii = 0;while (sum[ii] == 0x8080808080808080)ii++;int num = 0, sumnum = 0;for (; ii < n*n; ii++){if (sum[ii] == sum[ii - 1])num++;else{sumnum += num*(num + 1) / 2;num = 0;}}cout << sumnum << endl;}return 0;}


1 0
原创粉丝点击