GYM-G
思路:皮克定理 : s = in + on / 2 - 1
其中 s为多边形面积, in为在多边形内部的点数,on为在多边形边界上的点数 。都为整数点数。
根据叉积求面积。
on可以通过 gcd( a.x - b.x , a.y - b.y )得到,一个线段上的整数点数(gcd( a.x - b.x , a.y , b.y ))。
Code:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int AX = 1e5 + 666 ;
struct Node{
LL x , y ;
double operator ^ ( const Node &b)const{
return x * b.y - y * b.x ;
}
}p[AX];
int n ;
int gcd( int a , int b ){
return !b ? a : gcd( b , a % b ) ;
}
double getArea(){
double res = 0.0 ;
for(int i = 0; i < n ; i++)
res += ( p[i] ^ p[(i+1)%n] ) / 2 ;
return fabs(res);
}
int main(){
scanf("%d",&n) ;
for( int i = 0 ; i < n ; i++ ){
scanf("%lld%lld",&p[i].x,&p[i].y) ;
}
double s = getArea() ;
LL on = 0LL ;
for( int i = 0 ; i < n ; i++ ){
on += gcd( abs( p[i].x - p[(i+1)%n].x ) , abs( p[i].y - p[(i+1)%n].y ) ) ;
}
LL in = (LL)( 2 * s + 2 - on ) / 2LL ; // s = in + on / 2 - 1.0 ;
printf("%lld\n",in);
return 0 ;
}