[USACO3.4.2]Electric Fence

本文介绍了一个计算平面内三角形围栏能容纳多少整点(不包括边界)的问题,并使用皮克定理解决此问题。通过给出的代码示例,展示了如何利用最大公约数(GCD)来确定边界上的整点数量。

摘要生成于 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),(m,n),(p,0)
问三角形内有多少顶点
显然,皮克定理一发就行了。不用多说。

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

当然要利用gcd的性质求顶点有多少点。
也就是如果有
(0,0)到(m,n)————>其它2组同理
那么有gcd(m,n)+1就是经过的整点的个数(包括两个端点)
设gcd(m,n) = d;
因为m,n是满足条件的
所以m/d,n/d也是满足条件的。

/*
ID:cqz15311 
LANG:C++ 
PROG:fence9
*/
#include<bits/stdc++.h>
using namespace std;  
int gcd(int a,int b){
	if(!b)return a;else return gcd(b,a%b);  
}  
int main(){  
    freopen("fence9.in","r",stdin);  
    freopen("fence9.out","w",stdout);  
    int n,m,p;  
    scanf("%d%d%d",&n,&m,&p);
    int s,a,b;  
    b=0;  
    b+=gcd(n,m);
    b+=gcd(abs(p-n),m);
    b+=p;  
    s=(p*m) / 2;  
    a=s-b / 2+1;  
    printf("%d\n",a);
    return 0;
}  
/*
Executing...
   Test 1: TEST OK [0.000 secs, 4176 KB]
   Test 2: TEST OK [0.000 secs, 4176 KB]
   Test 3: TEST OK [0.000 secs, 4176 KB]
   Test 4: TEST OK [0.000 secs, 4176 KB]
   Test 5: TEST OK [0.000 secs, 4176 KB]
   Test 6: TEST OK [0.000 secs, 4176 KB]
   Test 7: TEST OK [0.000 secs, 4176 KB]
   Test 8: TEST OK [0.000 secs, 4176 KB]
   Test 9: TEST OK [0.000 secs, 4176 KB]
   Test 10: TEST OK [0.000 secs, 4176 KB]
   Test 11: TEST OK [0.000 secs, 4176 KB]
   Test 12: TEST OK [0.000 secs, 4176 KB]

All tests OK.
*/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值