結果

問題 No.1999 Lattice Teleportation
ユーザー 沙耶花沙耶花
提出日時 2022-07-01 23:39:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 164 ms / 2,000 ms
コード長 4,201 bytes
コンパイル時間 2,485 ms
コンパイル使用メモリ 214,984 KB
実行使用メモリ 12,536 KB
最終ジャッジ日時 2024-05-04 18:10:08
合計ジャッジ時間 5,307 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 109 ms
12,408 KB
testcase_13 AC 164 ms
12,508 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 162 ms
12,408 KB
testcase_16 AC 59 ms
8,188 KB
testcase_17 AC 99 ms
11,572 KB
testcase_18 AC 92 ms
10,876 KB
testcase_19 AC 101 ms
11,348 KB
testcase_20 AC 157 ms
12,536 KB
testcase_21 AC 160 ms
12,496 KB
testcase_22 AC 152 ms
12,412 KB
testcase_23 AC 9 ms
5,376 KB
testcase_24 AC 36 ms
6,108 KB
testcase_25 AC 81 ms
10,728 KB
testcase_26 AC 70 ms
9,044 KB
testcase_27 AC 56 ms
8,188 KB
testcase_28 AC 95 ms
11,920 KB
testcase_29 AC 45 ms
7,348 KB
testcase_30 AC 21 ms
5,376 KB
testcase_31 AC 16 ms
5,376 KB
testcase_32 AC 63 ms
8,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using pll = pair<long long, long long>;

// modint
template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) val += MOD;
    }
    constexpr int getmod() const { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b, swap(a, b);
            u -= t * v, swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr istream& operator >> (istream& is, Fp<MOD>& x) noexcept {
        is >> x.val;
        x.val %= MOD;
        if (x.val < 0) x.val += MOD;
        return is;
    }
    friend constexpr ostream& operator << (ostream& os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD>& r, long long n) noexcept {
        if (n == 0) return 1;
        if (n < 0) return modpow(modinv(r), -n);
        auto t = modpow(r, n / 2);
        t = t * t;
        if (n & 1) t = t * r;
        return t;
    }
    friend constexpr Fp<MOD> modinv(const Fp<MOD>& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b, swap(a, b);
            u -= t * v, swap(u, v);
        }
        return Fp<MOD>(u);
    }
};

const int MOD = 1000000007;
using mint = Fp<MOD>;

mint calc_area(vector<pll> vp) {
    int N = vp.size();
    mint res = 0;
    for (int i = 0; i < N; ++i) {
        mint px = vp[i].first, py = vp[(i+1)%N].first;
        mint qx = vp[i].second, qy = vp[(i+1)%N].second;
        res += px * qy - py * qx;
    }
    return res;
}

long long GCD(long long x, long long y) {
    if (y == 0) return x;
    else return GCD(y, x % y);
}

int main() {
    int N;
    cin >> N;
    vector<long long> X(N), Y(N);
    for (int i = 0; i < N; ++i){
		cin >> X[i] >> Y[i];
		if(Y[i]<0){
			X[i]*=-1;
			Y[i]*=-1;
		}
	}
	{
		vector<long long> x,y;
		for(int i=0;i<N;i++){
			if(X[i]!=0||Y[i]!=0){
				x.push_back(X[i]);
				y.push_back(Y[i]);
			}
		}
		swap(x,X);
		swap(y,Y);
		N = X.size();
	}
	if(N==0){
		cout<<1<<endl;
		return 0;
	}
    vector<int> ids(N);
    iota(ids.begin(), ids.end(), 0);
    sort(ids.begin(), ids.end(), [&](int i, int j) {
        return Y[i]*X[j] < Y[j]*X[i];
    });
    vector<pll> vp;
    vp.emplace_back(0, 0);
    for (auto i : ids) {
        auto p = vp.back();
        vp.emplace_back(p.first + X[i], p.second + Y[i]);
    }
    auto p = vp.back();
    int M = vp.size();
    for (int i = 1; i < M-1; ++i) {
        vp.emplace_back(p.first - vp[i].first, p.second - vp[i].second);
    }

    mint S = calc_area(vp);
    mint B = 0;
    for (int i = 0; i < vp.size(); ++i) {
        long long dx = vp[(i+1)%vp.size()].first - vp[i].first;
        long long dy = vp[(i+1)%vp.size()].second - vp[i].second;
        dx = abs(dx), dy = abs(dy);
        long long g = GCD(dx, dy);
        B += g;
    }
    mint res = (S + B) / 2;
	res = res+1;
    cout << res << endl;
}
0