HDU6055(Regular polygon)

来源:互联网 发布:批量图片透明度软件 编辑:程序博客网 时间:2024/06/05 06:23

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6055

有500个整数点的坐标,问总共欧多少个正多边形。

一个结论:若一个正多边形的点都是整数点,那么该正多边形是正四边形(具体证明见杨景钦在2017的国家队论文)。

首先,把点排序。

然后,枚举两个点,利用向量平行和垂直的关系以及正四边形各边长度相等计算出另外两个点,二分判断这两个点是否存在。

最后累加结果除以4(正四边形的每个边都枚举过一遍,所以除以4)。

#include <iostream>#include <cstdio>#include <algorithm>using namespace std;const int maxn = 510;struct Point{    int x, y;    Point() {}    Point(int x, int y):x(x), y(y) {}};typedef Point Vector;Point a[maxn];int n;Vector operator - (Point A, Point B){    return Vector(A.x-B.x, A.y-B.y);}bool operator == (Point A, Point B){    return A.x == B.x && A.y == B.y;}int Dot(Vector A, Vector B){    return A.x*B.x + A.y*B.y;}int Cha(Vector A, Vector B){    return A.x*B.y - A.y*B.x;}int cmp(const Point& A, const Point& B){    if (A.x != B.x){        return A.x < B.x;    }else{        return A.y < B.y;    }}bool Bin(Point key){    int l = 0;    int r = n-1;    while (l <= r){        int m = (l + r) >> 1;        if (key == a[m]){            return true;        }        if (cmp(a[m], key)){            l = m + 1;        }else{            r = m - 1;        }    }    return false;}int main(){    while (~scanf("%d", &n)){        for (int i=0; i<n; i++) scanf("%d%d", &a[i].x, &a[i].y);        sort(a, a+n, cmp);        int cnt = 0;        for (int i=0; i<n; i++)            for (int j=i+1; j<n; j++)        {            Point A = a[i];            Point B = a[j];            Vector AB = A - B;            Point C1 = Point(B.x + AB.y, B.y - AB.x);            Point C2 = Point(B.x - AB.y, B.y + AB.x);            Point D1 = Point(A.x + AB.y, A.y - AB.x);            Point D2 = Point(A.x - AB.y, A.y + AB.x);            Vector CD1 = C1 - D1;            Vector CD2 = C2 - D2;            if (Cha(CD1, AB) != 0){                Point T = C1; C1 = C2; C2 = C1;                CD1 = C1 - D1;                CD2 = C2 - D2;            }            if (Bin(C1) && Bin(D1)){                cnt++;            }            if (Bin(C2) && Bin(D2)){                cnt++;            }        }        cout << cnt/4 << endl;    }    return 0;}