제출 #1165897

#제출 시각아이디문제언어결과실행 시간메모리
1165897Math4Life2020Portal (BOI24_portal)C++20
11 / 100
15 ms2496 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long; using pii = pair<ll,ll>;
const ll INF = 1e18;

ll gcd2(ll a, ll b) {
    a = labs(a); b = labs(b);
    if (a==0) {
        return b;
    }
    if (b==0) {
        return a;
    }
    return gcd(a,b);
}

ll inv(ll x, ll MOD) {
    x = (x+MOD*(2-x/MOD))%MOD;
    array<ll,3> a = {x,1,0};
    array<ll,3> b = {MOD,0,1};
    while (b[0]!=1) {
        if (a[0]>b[0]) {
            swap(a,b);
        } else {
            ll k = b[0]/a[0];
            for (ll i=0;i<3;i++) {
                b[i]-=k*a[i];
            }
        }
    }
    ll vf = b[1];
    vf = (vf+MOD*(2-vf/MOD))%MOD;
    return vf;
}

pair<pii,pii> fz(pair<pii,pii> A0, pii pt) {
    pii p0 = A0.first; pii p1 = A0.second;
    if (A0.second.first==-INF) {
        if (p0.first*pt.second!=p0.second*pt.first) {
            return {p0,pt};
        } else {
            return A0;
        }
    }
    ll G = labs(p0.first*p1.second-p0.second*p1.first);
    ll G0 = labs(p0.first*pt.second-p0.second*pt.first);
    ll G1 = labs(p1.first*pt.second-p1.second*pt.first);
    if (G0%G==0 && G1%G==0) {
        return A0;
    }
    //else we can get a better gcd!
    ll L1 = p0.first*p1.second-p0.second*p1.first;
    ll L2 = p0.first*pt.second-p0.second*pt.first;
    ll LG = gcd2(L1,L2);
    L1 = L1/LG; L2 = L2/LG;
    ll A = inv(L1,L2);
    ll B = inv(L2,L1);
    pii pfn = {A*p0.first+B*p0.first,A*p0.second+B*p0.second};
    return {p0,pfn};
}


int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
	ll N; cin >> N;
    ll ans = 0;
    if (N<=2) {
        cout << "-1\n"; exit(0);
    }
    vector<pii> v0;
    pii p0;
    ll x1,y1; cin >> x1 >> y1;
    p0 = {x1,y1};
    ll g1t = 0; ll g2t = 0;
    for (ll i=1;i<N;i++) {
        ll x2,y2; cin >> x2 >> y2;
        v0.push_back({x2-x1,y2-y1});
        g1t = gcd2(g1t,x2-x1);
        g2t = gcd2(g2t,y2-y1);
    }
    if (g1t == 0 || g2t == 0) {
        cout << "-1\n"; exit(0);
    }
    pair<pii,pii> mem;
    mem = {v0[0],{-INF,-INF}};
    for (ll i=1;i<(N-1);i++) {
        mem = fz(mem,v0[i]);
    }
    if (mem.second.first==-INF) {
        cout << "-1\n"; exit(0);
    } else {
        cout << labs(mem.first.first*mem.second.second-mem.first.second*mem.second.first) << "\n";
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...