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

探讨如何计算由特定三角形围栏包围的格点区域中能够放置的牛的数量,利用皮克定理解决具体问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值