【USACO 3.4】(fence9)Electric Fence
-------------Prob--------------
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?
--------------Solution-------------
首先吐槽一下。。。
为什么做完fence之后就是fence4完了还有fence9啊。。。。
真是不会取名字。。。
本题是个数学题,提供关键词“皮克定理”,小学奥数内容了
然后请自行证明过原点和(n,m)的直线上的格点数是gcd(n,m)+1
最后整理公式
--------------Code-----------------
//真的还有必要上Code么、。、
/*
ID:zst_0110
LANG:C++
TASK:fence9
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int gcd(int a,int b)
{
int r;
while(b>0)
{
r=a%b;
a=b;
b=r;
}
return a;
}
int main()
{
int n,m,p;
freopen("fence9.in","r",stdin);
freopen("fence9.out","w",stdout);
//s=a+b/2-1
scanf("%d%d%d",&n,&m,&p);
printf("%d\n",((p*m)/2)+1-(((gcd(n,m)+1)+(gcd(abs(n-p),m)+1)+p-2)/2));
return 0;
}