結果

問題 No.2769 Number of Rhombi
ユーザー srjywrdnprkt
提出日時 2024-05-08 19:23:45
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 802 ms / 5,000 ms
コード長 1,486 bytes
コンパイル時間 2,323 ms
コンパイル使用メモリ 211,492 KB
最終ジャッジ日時 2025-02-21 11:48:24
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

//fraction (p/q)
struct fraction{
    ll p, q;
    fraction(ll P=0, ll Q=1) : p(P), q(Q) {
        ll g = gcd(P, Q);
        p /= g; q /= g;
        if (q < 0){
            p *= -1;
            q *= -1;
        }
        if (q == 0) p = 1;
        if (p == 0) q = 1;
    }
    bool operator < (const fraction & other) const{
        if (p < 0) return -p*other.q > -other.p*q;
        return p*other.q < other.p*q;
    }
    bool operator == (const fraction & other) const{
        return p*other.q == other.p*q;
    }
    bool operator > (const fraction & other) const{
        if (p < 0) return -p*other.q < -other.p*q;
        return p*other.q > other.p*q;
    }
    pair<ll, ll> top() const{
        return {p, q};
    }
};


int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    ll N, ans=0, s, t, a, b, c, d;
    cin >> N;
    vector<ll> x(N), y(N);
    for (int i=0; i<N; i++) cin >> x[i] >> y[i];

    map<tuple<ll, ll, ll, ll>, ll> mp;
    for (int i=0; i<N; i++){
        for (int j=i+1; j<N; j++){
            fraction p(y[i]-y[j], x[i]-x[j]);
            tie(s, t) = p.top();
            mp[{x[i]+x[j], y[i]+y[j], s, t}]++;
        }
    }

    for (auto &[g, h] : mp){
        tie(a, b, c, d) = g;
        fraction q = fraction(-d, c);
        tie(s, t) = q.top();
        if (mp.count({a, b, s, t})) ans += mp[{a, b, s, t}] * h;
    }

    cout << ans/2 << endl;

    return 0;
}
0