gcd(abs(x1-x2),abs(y1-y2))
答案为线段(x1,y1)(x2,y2)线段格点个数-1;
pick公式:A=I+E/2-1;
A:面积
I:多边形内格点个数
E:多边形边上格点总数
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const double eps=1e-8;
struct P
{
int x,y;
P (){}
P (int x,int y):x(x),y(y){
}
P operator +(P p){
return P(x+p.x,y+p.y);
}
P operator -(P p){
return P(x-p.x,y-p.y);
}
int det(P p){
return x*p.y-y*p.x;
}
int dot(P p){
return x*p.x+y*p.y;
}
};
int gcd(int a,int b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
int t,n;
P r,p,v;
int main()
{
int step=1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
scanf("%d%d",&p.x,&p.y);
v=p;
double ans=0;
int e=0,I=0;
for(int i=1;i<n;i++)
{
scanf("%d%d",&r.x,&r.y);
r.x+=p.x;
r.y+=p.y;
e+=gcd(abs(r.x-p.x),abs(r.y-p.y));
ans+=(double)(p.x*r.y-p.y*r.x);
p=r;
}
e+=gcd(abs(r.x-v.x),abs(r.y-v.y));
ans+=(double)(p.x*v.y-p.y*v.x);
ans/=2.0;
ans=fabs(ans);
I=ans+1-e/2;
if(step!=1)
printf("\n");
printf("Scenario #%d:\n",step++);
printf("%d %d %.1lf\n",I,e,ans);
}
return 0;
}