H - 三角形
4.1 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).
4.2 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. For example:
0 0 1 0 0 1 0 0 5 0 0 5 0 0 0 0 0 0
4.3 Output
For each input case, the program should print the number of internal lattice points on a single line. For example:
0 6
思路:該題求的是三角形內部(不包含邊界的)的整點,需要運用到的皮克定理。什么是皮克定理呢?
2*S=2*A+B-2
(S為三角形面積,A為三角形內部的整點數,B為三角形邊上整點數)
那么問題來了,三角形面積如何求?海倫公式?並不是,這里需要運用2S=x1y2+x2y3+x3y1-x1y3-x2y1-x3y2
定點數如何求?兩點之間坐標相減並求它們的最大公因數:b=∑gcd(|xi-x[(i+1)%3]|,|yi-y[(i+1)%3]|)
a=(2S-b+2)/2
接下來是代碼部分:

1 #include <iostream> 2 #include<cmath> 3 #include<cstdlib> 4 #include<cstring> 5 #include<string> 6 #include <algorithm> 7 using namespace std; 8 typedef long long ll; 9 10 struct point 11 { 12 int x,y; 13 }p[5]; 14 int area() 15 { 16 int ans=0; 17 for(int i=0 ; i<3 ; i++) 18 { 19 ans+=p[i].x*(p[(i+1)%3].y-p[(i+2)%3].y); 20 } 21 return ans; 22 } 23 int gcd(int a,int b) 24 { 25 return b==0?a:gcd(b,a%b); 26 } 27 int atline (point p1,point p2) 28 { 29 int b=fabs(p1.x-p2.x) , a=fabs(p1.y-p2.y); 30 return gcd(a,b); 31 } 32 int main () 33 { 34 bool flag; 35 int i,n,s,lpoint; 36 while (1) 37 { 38 flag=1; 39 for ( i=0 ; i<3 ; i++) 40 scanf("%d%d",&p[i].x,&p[i].y); 41 for ( i=0 ; i<3 ; i++) 42 if ( p[i].x || p[i].y )flag=0; 43 if(flag) break; 44 s=fabs(area())+2; 45 for (i=0 ; i<3 ; i++) 46 { 47 lpoint+=atline(p[i],p[(i+1)%3]); 48 } 49 int ans=(s-lpoint)/2; 50 printf("%d\n",ans); 51 } 52 return 0; 53 }