# 皮克定理及其应用

## 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 }
View Code

posted @ 2019-04-15 23:54  三玖是天  阅读(...)  评论(...编辑  收藏