USACO3.4.3--Electric Fence
Don Piele
In this problem, `lattice points' in the plane are points with integer coordinates.
In order to contain his cows, Farmer John constructs a triangular electric fence by stringing a "hot" wire from the origin (0,0) to a lattice point [n,m] (0<=;n<32000, 0<m<32000), then to a lattice point on the positive x axis [p,0] (p>0), and then back to the origin (0,0).
A cow can be placed at each lattice point within the fence without touching the fence (very thin cows). Cows can not be placed on lattice points that the fence touches. How many cows can a given fence hold?
PROGRAM NAME: fence9
INPUT FORMAT
The single input line contains three space-separated integers that denote n, m, and p.
SAMPLE INPUT (file fence9.in)
7 5 10
OUTPUT FORMAT
A single line with a single integer that represents the number of cows the specified fence can hold.
SAMPLE OUTPUT (file fence9.out)
20
题解:对于这种计算几何题完全没想法啊,题解上说用皮克定理,然后去wikipedia上看了一下这个定理,是这样的:给定顶点座标均是整点(或正方形格点)的简单多边形,皮克定理说明了其面积A和内部格点数目i、边上格点数目b的关系:A = i+ b/2 - 1。有了这个公式我们就可以轻松秒杀这题了,我们只需算出面积A和边上的格点数b即可,但有个问题就是,如何求边上的格点?最朴素的方法就是计算出两条线段上的整数解(貌似扩展欧几里得?),然后加上X轴的坐标即可,网上题解说可以证明,一条直线((0,0),(n,m))上的格点数等于n与m的最大公约数+1。(具体证明怎么样的我也不知道。。记住好了。。。。)到此为止,我们就完美的解决了这道问题。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 ID:spcjv51 3 PROG:fence9 4 LANG:C 5 */ 6 #include<stdio.h> 7 #include<stdlib.h> 8 int gcd(int a,int b) 9 { 10 if(b==0) 11 return a; 12 else 13 return gcd(b,a%b); 14 } 15 int main(void) 16 { 17 freopen("fence9.in","r",stdin); 18 freopen("fence9.out","w",stdout); 19 int n,m,p,b,i,a; 20 scanf("%d%d%d",&n,&m,&p); 21 b=0; 22 b+=gcd(n,m)+1; 23 b+=gcd(abs(n-p),m)+1; 24 b=b+p-2; 25 a=(p*m)/2; 26 i=a+1-b/2; 27 printf("%d\n",i); 28 return 0; 29 }