题目:在整数构成的二维平面上,统计多边形内整数坐标点的个数。
分析:计算几何、pick定理。根据pick定理,我们有在坐标为整数的二维平面内,有s=a+b/2-1,其中b是落在边上的点数,a是内部点数。由此可知:a=s-b/2+1,其中s可利用叉乘求解;b利用gcd求解即可。
注意:面积要取绝对值。
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
typedef long long LL;
typedef struct pnode{
LL x,y;
}point;
point P[1005];
LL ab( LL v )
{
if ( v >= 0LL ) return v;
else return 0LL-v;
}
LL gcd( LL a, LL b )
{
if ( b == 0LL ) return a;
return a%b?gcd(b,a%b):b;
}
LL crossproduct( point a, point b, point c )
{
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y)+0LL;
}
int main()
{
int N;
while ( cin >> N && N ) {
for ( int i = 0 ; i < N ; ++ i )
cin >> P[i].x >> P[i].y;
P[N] = P[0];
LL a,b,r,count = 0LL;
for ( int i = 0 ; i < N ; ++ i ) {
a = ab( P[i+1].x - P[i].x );
b = ab( P[i+1].y - P[i].y );
r = gcd( a, b );
count += r + 0LL;
}
LL area = 0LL;
for ( int i = 2 ; i < N ; ++ i )
area += crossproduct( P[0], P[i-1], P[i] );
cout << ab(area)/2LL+1LL-count/2LL << endl;
}
return 0;
}