PRAVO - Tam giác vuông

Tác giả: ladpro98

Ngôn ngữ: C++

#include <iostream>
#include <cstdio>
#include <algorithm>
#define ii pair<int, int>
#define X first
#define Y second
const int N = 1555;
const long double eps = 1e8;
using namespace std;
ii v[N];
ii a[N];
int n, top;

int gcd(int a, int b) {
    while (b) a %= b, swap(a, b); return a;
}

void refine(ii &v) {
    int g = gcd(v.X, v.Y);
    v.X /= g; v.Y /= g;
    if (v.X < 0) {
        v.X = -v.X; v.Y = -v.Y;
    }
}

int main() {
    ios :: sync_with_stdio(0);
    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> a[i].X >> a[i].Y;
    int res = 0;
    ii t;
    for(int i = 1; i <= n; i++) {
        top = 0;
        for(int j = 1; j <= n; j++)
        if (i != j)
            v[top++] = ii(a[i].X - a[j].X, a[i].Y - a[j].Y);
        for(int j = 0; j < top; j++) refine(v[j]);
        sort(v, v + top);
        for(int j = 0; j < top; j++) {
            t = ii(v[j].Y, -v[j].X);
            if (t.X < 0) {t.X = -t.X; t.Y = -t.Y;}
            res += upper_bound(v + j + 1, v + top, t) -
                   lower_bound(v + j + 1, v + top, t);
        }
    }
    cout << res;
    return 0;
}

Download