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<32,000, 0<m<32,000), then to a lattice point on the positive x axis [p,0] (0<p<32,000), 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.
OUTPUT FORMAT
A single line with a single integer that represents the number of cows the specified fence can hold.
描述
在本题中,格点是指横纵坐标皆为整数的点。
为了圈养他的牛,农夫约翰(Famer John)建造了一个三角形的电网。他从原点(0,0)牵出一根通电的电线,连接格点(n,m)(0<=n<32000,0<m<32000),再连接格点(p,0)(p>0),最后回到原点。
牛可以在不碰到电网的情况下被放到电网内部的每一个格点上(十分瘦的牛)。如果一个格点碰到了电网,牛绝对不可以被放到该格点之上(或许Famer John会有一些收获)。那么有多少头牛可以被放到农夫约翰的电网中去呢?
格式
PROGRAM NAME: fence9
INPUT FORMAT:
(file fence9.in)
输入文件只有一行,包含三个用空格隔开的整数:n,m和p。
OUTPUT FORMAT:
(file fence9.out)
输出文件只有一行,包含一个整数,代表能被指定的电网包含的牛的数目。
SAMPLE INPUT
7 5 10
SAMPLE OUTPUT
20
前言:咳咳,正当我向大视野、PKU等众多题库征战的时候,我猛然发现最最经典的USACO原题我竟然还没有刷完!!!于是最近我要把这个刷到5、6版,并且会陆续写一些题解。(水题就不解释了)
预备知识:皮克定理
对于这道题目,肯定很多人开始都束手无策,但是看到这个公式后就豁然开朗。我们先求出这个三角形的面积和位于三角形边上的点(GCD来求),在反向套用皮克定理,求出其内部的点。
代码:
{
ID:juan1973
LANG:PASCAL
PROG:fence9
}
var
n,m,p,on_the_line,left_up,right_up,ans:longint;
function gcd(a,b:longint):longint;
begin
if a mod b=0 then exit(b);
exit(gcd(b,a mod b));
end;
begin
assign(input,'fence9.in');
assign(output,'fence9.out');
reset(input);
rewrite(output);
readln(n,m,p);
if n=0 then left_up:=m else left_up:=gcd(n,m);
if abs(n-p)>0 then right_up:=gcd(abs(n-p),m) else right_up:=m;
on_the_line:=left_up+right_up+p;
ans:=(p*m-on_the_line) div 2+1;
writeln(ans);
close(input);
close(output);
end.