匹克定理
皮克定理是指一个计算点阵中顶点在格点上的多边形面积公式,该公式可以表示为2S=2a+b-2,其中a表示多边形内部的点数,b表示多边形边界上的点数,S表示多边形的面积。
例题:G. Water Testing(cf)
代码:
1 #include <bits/stdc++.h>
2 using namespace std;
3 typedef long long ll;
4 const int maxn = 1e5 + 10;
5 struct node
6 {
7 ll x, y;
8 node operator- (const node& b)
9 {
10 return { x - b.x, y - b.y };
11 }
12 }a[maxn];
13
14 ll mul(node b, node c)
15 {
16 return (b.x * c.y - b.y * c.x);
17 }
18
19 ll gcd(int a, int b)
20 {
21 return !b ? a : gcd(b, a % b);
22 }
23
24 ll res(node a, node b)
25 {
26 if (a.x == b.x) return abs(a.y - b.y) - 1;
27 if (a.y == b.y) return abs(a.x - b.x) - 1;
28 return gcd(abs(a.x - b.x), abs(a.y - b.y)) - 1;
29 }
30
31 int main()
32 {
33 int n; cin >> n;
34 ll s = 0;
35 ll num = n;
36
37 for (int i = 0; i < n; i++)
38 scanf("%lld%lld", &a[i].x, &a[i].y);
39 for (int i = 1; i < n - 1; i++)
40 s += mul(a[i] - a[0], a[i + 1] - a[0]);
41 s = abs(s);
42 for (int i = 0; i < n; i++)
43 num += res(a[i], a[(i + 1) % n]);
44 //cout << s << " " << num << endl;
45 cout << (s - num + 2) / 2 << endl;
46 }