跳到正文

题解

题解:P12856 [NERC 2020 Online] Geometrical Combinatorics

heyuanzhi约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 4 分钟
题目
P12856

版本

  1. v1当前

#include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long ll; const ll MOD = 1e9 + 7;

struct Point { ll x, y; Point(ll x = 0, ll y = 0) : x(x), y(y) {} };

// 计算向量叉积 ll cross(const Point& a, const Point& b) { return a.x * b.y - a.y * b.x; }

// 判断点p是否在三角形abc内部或边界上 bool isInside(const Point& p, const Point& a, const Point& b, const Point& c) { ll s1 = cross(Point(b.x - a.x, b.y - a.y), Point(p.x - a.x, p.y - a.y)); ll s2 = cross(Point(c.x - b.x, c.y - b.y), Point(p.x - b.x, p.y - b.y)); ll s3 = cross(Point(a.x - c.x, a.y - c.y), Point(p.x - c.x, p.y - c.y)); bool allNonNegative = (s1 >= 0 && s2 >= 0 && s3 >= 0); bool allNonPositive = (s1 <= 0 && s2 <= 0 && s3 <= 0); return allNonNegative || allNonPositive; }

// 计算逆元(费马小定理) ll modInverse(ll a, ll mod) { ll res = 1; ll exponent = mod - 2; ll base = a % mod;

代码
while (exponent > 0) {
    if (exponent % 2 == 1) {
        res = (res * base) % mod;
    }
    base = (base * base) % mod;
    exponent /= 2;
}
return res;

}

// 计算组合数C(n, k) mod MOD(修正版) ll comb(ll n, ll k) { if (k < 0 || k > n) return 0; k = min(k, n - k);

C++
ll numerator = 1;
for (ll i = n; i > n - k; --i) {
    numerator = (numerator * i) % MOD;
}

ll denominator = 1;
for (ll i = 1; i <= k; ++i) {
    denominator = (denominator * i) % MOD;
}

return (numerator * modInverse(denominator, MOD)) % MOD;

}

int main() { int t; cin >> t; while (t—) { ll xA, yA, xB, yB, xC, yC; cin >> xA >> yA >> xB >> yB >> xC >> yC; Point a(xA, yA), b(xB, yB), c(xC, yC);

C++
    // 确定y的范围
    ll yMin = min({a.y, b.y, c.y});
    ll yMax = max({a.y, b.y, c.y});
    yMin = max(yMin, 0LL); // 帕斯卡三角形只在上半平面

    ll sum = 0;
    for (ll y = yMin; y <= yMax; ++y) {
        // 确定x的范围为[-y, y],步长为2
        for (ll x = -y; x <= y; x += 2) {
            Point p(x, y);
            if (isInside(p, a, b, c)) {
                ll k = (x + y) / 2;
                sum = (sum + comb(y, k)) % MOD;
            }
        }
    }
    cout << sum << endl;
}
return 0;

}

原文