欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Water Testing

程序员文章站 2022-03-29 21:19:36
...

题目描述
You just bought a large piece of agricultural land, but you noticed that – according to regulations – you have to test the ground water at specific points on your property once a year. Luckily the description of these points is rather simple. The whole country has been mapped using a Cartesian Coordinate System (where (0, 0) is the location of the Greenwich Observatory). The corners of all land properties are located at integer coordinates according to this coordinate system. Test points for ground water have to be erected on every point inside a property whose coordinates are integers.

输入
The input consists of:
• one line with a single integer n (3 ≤ n ≤ 100 000), the number of corner points of your property;
• n lines each containing two integers x and y (−106 ≤ x, y ≤ 106 ), the coordinates of each corner.
The corners are ordered as they appear on the border of your property and the polygon described by the points does not intersect itself.

输出
The number of points with integer coordinates that are strictly inside your property.

样例输入
复制样例数据
4
0 0
0 10
10 10
10 0
样例输出
81

思路:运用了皮克定理
皮克定理是指一个计算点阵中顶点在格点上的多边形面积公式,该公式可以表示为2S=2a+b-2,其中a表示多边形内部的点数,b表示多边形边界上的点数,S表示多边形的面积。
这个b的求法是gcd(abs(x2-x1),abs(y2-y1))

#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
typedef long long ll;
const int mod = 1e+9+7;
const int N = 1e+6+5;

int n;
ll a[N][2];

ll fun(ll x1,ll y1,ll x2,ll y2){
	return __gcd(abs(x2-x1),abs(y2-y1));
}

int main(){
	scanf("%d",&n);
	ll s = 0,y = 0;
	for(int i = 1;i <= n;i++){
		scanf("%lld %lld",&a[i][0],&a[i][1]);
	}
	a[n+1][0] = a[1][0],a[n+1][1] = a[1][1];
	for(int i = 1;i <= n;i++){
		s += a[i][0]*a[i+1][1]-a[i+1][0]*a[i][1];
		y += fun(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
	}
	s = abs(s);
	printf("%lld\n",(s+2-y)/2);
	return 0;
}
/*
3
0 0
3 3
3 0
*/

上一篇: testing

下一篇: 两亲性分子uva1606