結果

問題 No.1999 Lattice Teleportation
ユーザー 沙耶花沙耶花
提出日時 2022-07-01 23:39:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 4,201 bytes
コンパイル時間 2,151 ms
コンパイル使用メモリ 213,872 KB
実行使用メモリ 12,656 KB
最終ジャッジ日時 2023-08-17 11:26:31
合計ジャッジ時間 5,435 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 112 ms
12,224 KB
testcase_13 AC 166 ms
12,276 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 167 ms
12,312 KB
testcase_16 AC 61 ms
8,108 KB
testcase_17 AC 100 ms
11,448 KB
testcase_18 AC 91 ms
11,012 KB
testcase_19 AC 96 ms
12,212 KB
testcase_20 AC 167 ms
12,272 KB
testcase_21 AC 165 ms
12,656 KB
testcase_22 AC 166 ms
12,584 KB
testcase_23 AC 9 ms
4,376 KB
testcase_24 AC 38 ms
5,744 KB
testcase_25 AC 89 ms
11,060 KB
testcase_26 AC 73 ms
8,960 KB
testcase_27 AC 61 ms
8,220 KB
testcase_28 AC 103 ms
11,776 KB
testcase_29 AC 50 ms
7,120 KB
testcase_30 AC 22 ms
4,660 KB
testcase_31 AC 17 ms
4,560 KB
testcase_32 AC 67 ms
8,560 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