Cod sursa(job #2789648)

Utilizator andrei_C1Andrei Chertes andrei_C1 Data 27 octombrie 2021 19:19:54
Problema Trapez Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <unordered_map>
#include <chrono>

FILE *fin, *fout;

const int NMAX = 1e3;
const int INF = 1e9;

int N, ans;
std :: pair <int, int> v[NMAX + 1];

struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const std :: pair<T1, T2> &p) const {
        auto hash1 = std :: hash <T1> {} (p.first);
        auto hash2 = std :: hash <T2> {} (p.second);
        return hash1 ^ hash2;
    }
};

std :: unordered_map <std :: pair<int, int>, int, hash_pair> fr;

std :: pair<int, int> panta(std :: pair <int, int> a, std :: pair <int, int> b) {
    int gcd = std :: __gcd((a.second - b.second), (a.first - b.first));
    return {(a.second - b.second) / gcd, (a.first - b.first) / gcd};
}

int main() {
    fin = fopen("trapez.in", "r");
    fout = fopen("trapez.out", "w");

    fscanf(fin, "%d", &N);

    for(int i = 1; i <= N; i++) {
        int x, y;
        fscanf(fin, "%d %d", &x, &y);
        v[i] = {x, y};
    }

    for(int i = 1; i <= N; i++) {
        for(int j = i + 1; j <= N; j++) {
            fr[panta(v[i], v[j])]++;
        }
    }

    for(auto it: fr) {
        if(it.second < 2) {
            continue;
        }
        ans += it.second * (it.second - 1) >> 1;
    }

    fprintf(fout, "%d", ans);
    return 0;
}