您的位置:首页 > 编程语言 > C语言/C++

C++&Pascal&Python——【USACO 3.4.2】——Electric Fence

2017-10-09 20:08 357 查看
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.

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


在这个问题中,平面中的“格点”是具有整数坐标的点。

为了遏制他的母牛,农民约翰通过从原点(0,0)穿过一个“热”线到网格点[n,m](0 <=; n <32,000,0 <m <32,000),然后到正x轴上的点阵[p,0](0 <p <32,000),然后返回到原点(0,0)。

一只牛可以放置在栅栏内的每个格子点,而不会碰到栅栏(很瘦的牛)。 奶牛不能放在栅栏接触的格点上。 给定的围栏内有多少只牛?

程序名称:fence9

输入格式

单个输入行包含三个空格分隔的整数,表示n,m和p。

SAMPLE INPUT(文件fence9.in)

7 5 10
输出格式

单行,单个整数,表示指定围栏可以容纳的牛数。

SAMPLE OUTPUT(文件fence9.out)

20
Tips :

皮克定理是指一个计算点阵中顶点在格点上的多边形面积公式,该公式可以表示为2S=2a+b-2,其中a表示多边形内部的点数,b表示多边形边界上的点数,s表示多边形的面积。

/*
ID : mcdonne1
LANG : C++
TASK : fence9
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int gcd(int x,int y){return x%y==0 ? y : gcd(y,x%y);}
int main()
{
freopen("fence9.in","r",stdin);
freopen("fence9.out","w",stdout);
int n,m,p,b;
cin>>n>>m>>p;
b=gcd(n,m)+gcd(abs(p-n),m)+p;
cout<<p*m/2+1-b/2<<endl;
return 0;
}
{
ID : mcdonne1
LANG : PASCAL
TASK : fence9
}
var
n, m, p, b, a : longint;
function abs (a : longint) : longint;
begin
if a > 0 then exit (a)
else exit (-a);
end;
function gcd (x, y : longint) : longint;
begin
if x mod y = 0 then exit (y)
else exit (gcd (y, x mod y));
end;
begin
assign (input, 'fence9.in');
assign (output, 'fence9.out');
reset (input);
rewrite (output);
read (n, m, p);
b := gcd (n, m) + gcd (abs (p - n), m) + p;
a := p * m div 2 + 1 - b div 2;
writeln (a);
close (input);
close (output);
end.
"""
ID : mcdonne1
LANG : PYTHON
TASK : fence9
"""
def gcd (x, y) :
if x % y == 0 :
return y
else :
return gcd (y, x% y)
fin = open ('fence9.in', 'r')
fout = open ('fence9.out', 'w')
r = fin.readline().split()
n = int(r[0])
m = int(r[1])
p = int(r[2])
b = gcd(n, m) + gcd(abs(p - n), m) + p
a = p * m / 2 + 1 - b / 2
fout.write (str(a) + '\n')
fin.close()
fout.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: