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

来源:互联网 发布:mac系统顿号怎么打 编辑:程序博客网 时间:2024/06/07 07:49

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 : mcdonne1LANG : 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 : mcdonne1LANG : PASCALTASK : fence9}varn, 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 : mcdonne1LANG : PYTHONTASK : fence9"""def gcd (x, y) :if x % y == 0 :return yelse :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) + pa = p * m / 2 + 1 - b / 2fout.write (str(a) + '\n')fin.close()fout.close()