首页 > 其他 > 详细

usaco training 3.4.3 fence9 题解

时间:2014-02-26 03:53:35      阅读:279      评论:0      收藏:0      [点我收藏+]

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<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.


       前言:咳咳,正当我向大视野、PKU等众多题库征战的时候,我猛然发现最最经典的USACO原题我竟然还没有刷完!!!于是最近我要把这个刷到5、6版,并且会陆续写一些题解。(水题就不解释了)

预备知识:皮克定理

一张方格纸上,上面画着纵横两组平行线,相邻平行线之间的距离都相等,这样两组平行线的交点,就是所谓格点。如果取一个格点做原点O,如图1,取通过这个格点的横向和纵向两直线分别做横坐标轴OX和纵坐标轴OY,并取原来方格边长做单位长,建立一个坐标系。这时前面所说的格点,显然就是纵横两坐标都是整数的那些点。如图1中的O、P、Q、M、N都是格点。由于这个缘故,我们又叫格点为整点。
一个多边形的顶点如果全是格点,这多边形就叫做格点多边形。有趣的是,这种格点多边形的面积计算起来很方便,只要数一下图形边线上的点的数目及图内的点的数目,就可用公式算出。
这个公式是皮克(Pick)在1899年给出的,被称为“皮克定理”,这是一个实用而有趣的定理。
给定顶点坐标均是整点(或正方形格点)的简单多边
bubuko.com,布布扣

a=39,b=14,s=45

形,皮克定理说明了其面积S和内部格点数目a、边上格点数目b的关系:
S=a+b÷2-1。
(其中a表示多边形内部的点数,b表示多边形边界上的点数,S表示多边形的面积)
-----------------------------------------以上摘自百度百科---------------------------------------------

对于这道题目,肯定很多人开始都束手无策,但是看到这个公式后就豁然开朗。我们先求出这个三角形的面积和位于三角形边上的点(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.

usaco training 3.4.3 fence9 题解

原文:http://blog.csdn.net/jiangshibiao/article/details/19910987

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!