首页 > 代码库 > 【POJ】Triangle(pick定理)

【POJ】Triangle(pick定理)

http://poj.org/problem?id=2954

表示我交了20+次...

为什么呢?因为多组数据我是这样判断的:da=sum{a[i].x+a[i].y},然后!da就表示没有数据了QAQ我居然查了如此久都没查出来!!!!注意负数啊负数啊啊啊啊啊啊啊

本题是pick定理:当多边形的顶点均为整数时,面积=内部整点+边上整点/2-1

然后本题要求内部整点

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <queue>#include <set>#include <map>using namespace std;typedef long long ll;#define pii pair<int, int>#define mkpii make_pair<int, int>#define pdi pair<double, int>#define mkpdi make_pair<double, int>#define pli pair<ll, int>#define mkpli make_pair<ll, int>#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << (#x) << " = " << (x) << endl#define error(x) (!(x)?puts("error"):0)#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }#define printarr1(a, b) for1(_, 1, b) cout << a[_] << ‘\t‘; cout << endlinline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }struct Point { int x, y; }a[4];int area(Point &a, Point &b, Point &c) {	static int x1, x2, y1, y2;	x1=a.x-c.x; y1=a.y-c.y;	x2=b.x-c.x; y2=b.y-c.y;	return abs(x1*y2-x2*y1);}int gcd(int a, int b) { return b?gcd(b, a%b):a; }int onLine(Point &a, Point &b) {	static int x, y;	x=abs(a.x-b.x);	y=abs(a.y-b.y);	return gcd(x, y);}int main() {	while(1) {		int sum=0;		rep(i, 3) read(a[i].x), read(a[i].y), sum|=a[i].x|a[i].y;		if(!sum) return 0;		int on=onLine(a[0], a[1])+onLine(a[1], a[2])+onLine(a[2], a[0]);		printf("%d\n", (area(a[0], a[1], a[2])-on)/2+1);	}	return 0;}

  

 


 

 

Description

A lattice point is an ordered pair (x, y) where x and y are both integers. Given the coordinates of the vertices of a triangle (which happen to be lattice points), you are to count the number of lattice points which lie completely inside of the triangle (points on the edges or vertices of the triangle do not count).

Input

The input test file will contain multiple test cases. Each input test case consists of six integers x1, y1, x2, y2, x3, and y3, where (x1, y1), (x2, y2), and (x3, y3) are the coordinates of vertices of the triangle. All triangles in the input will be non-degenerate (will have positive area), and −15000 ≤ x1, y1, x2, y2, x3, y3 ≤ 15000. The end-of-file is marked by a test case with x1 =  y1 = x2 = y2 = x3 = y3 = 0 and should not be processed.

Output

For each input case, the program should print the number of internal lattice points on a single line.

Sample Input

0 0 1 0 0 10 0 5 0 0 50 0 0 0 0 0

Sample Output

06

Source

Stanford Local 2004

【POJ】Triangle(pick定理)