Water Testing【皮克定理,多边形面积,线段上点的数目】

Water Testing

传送门:链接  来源:UPC 9656

题目描述

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

题目大意:

给出一个多边形每个顶点的坐标,求在该图形内部方格点的个数。

解题思路:

`1、皮克定理:2S=b+2a-2   (其中S表示多边形的面积,b表示多边形上点的个数,a表示多边形内部点的个数。)

2、已知顶点坐标求多边形面积公式S=0.5*abs(x1*y2-y1*x2+x2*y3-y2*x3+...+xn*y1-yn*x1)

3、已知方向向量为(x,y)求在线段上点的个数b=gcd(fabs(x),fabs(y))

如果(x1,y1)只是一个顶点而不是向量,就要先求出边的向量才能用公式3!!!

根据上面三个公式可以求出: b=S-1+0.5*a

qi shi shu lun bu gui wo guan ! 

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAX=1e5;
struct point{ 
    double x;
    double y;
}p[MAX+5];
LL gcd(LL a,LL b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
int main()
{
    LL n;
    cin>>n;
    double s=0,b=0;
    for(LL i=0;i<n;i++){
        cin>>p[i].x>>p[i].y;
        if(i!=0){
            b+=gcd(fabs(p[i].x-p[i-1].x),fabs(p[i].y-p[i-1].y));
            s+=(p[i].y*p[i-1].x-p[i].x*p[i-1].y);
        }
    }
    s+=(p[0].y*p[n-1].x-p[0].x*p[n-1].y);
    b+=gcd(fabs(p[0].x-p[n-1].x),fabs(p[0].y-p[n-1].y)); 
    s=0.5*fabs(s);
    cout<<(LL)(s+1-b*0.5)<<endl;
    return 0;
}

 

posted @ 2019-08-18 21:00  XJHui  阅读(190)  评论(0编辑  收藏  举报