Cod sursa(job #3338899)

Utilizator parus_majorParus Major parus_major Data 5 februarie 2026 13:35:53
Problema Trapez Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <climits>
#include <unordered_map>

using namespace std;

ifstream fin("trapez.in");
ofstream fout("trapez.out");

const int MAXN = 1002;

struct pairhash {
public:
    template <typename T, typename U>
    std::size_t operator()(const std::pair<T, U>& x) const {
        return std::hash<T>()(x.first) ^ std::hash<U>()(x.second);
    }
};

unordered_map<pair<int, int>, int, pairhash> cnt;
pair<int, int> v[MAXN];
int N;

pair<int, int> compute(int a, int b) {
    if (a < 0) { a = -a; b = -b; }
    if (b == 0) return { 1, 0 };
    if (a == 0) return { 0, 1 };
    
    int x = abs(a);
    int y = abs(b);
    int r = 0;
    while (y) {
        r = x % y;
        x = y;
        y = r;
    }
    return { a / x, b / x };
}

int main()
{
    fin >> N;
    for (int i = 0; i < N; ++i)
        fin >> v[i].first >> v[i].second;

    for (int i = 0; i < N; ++i) {
        for (int j = i + 1; j < N; ++j) {
            cnt[compute(v[i].first - v[j].first, v[i].second - v[j].second)]++;
        }
    }
    long long ans = 0LL;
    for (auto& [_, y] : cnt) {
        ans = ans + static_cast<long long>(y) * (y - 1) / 2;
    }
    fout << ans;
}