TZOJ 5103: Electric Fence(电子围栏+皮克定理)

描述

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?

输入

The single input line contains three space-separated integers that denote n, m, and p.

输出

A single line with a single integer that represents the number of cows the specified fence can hold.

样例输入

7 5 10

样例输出

20

题目大意:

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

为了容纳他的奶牛,农夫建造了一个三角形的电栅栏,将一根“热”线从原点(0,0)串到格点[n,m](0<=n<32,000,0<m<32,000),然后到x轴 [p,0](0<p<32,000)上的格点,然后回到原点(0,0)。

可以在围栏内的每个格子点放置一头牛,而无需接触围栏(非常小的奶牛,无视体积就完了)。奶牛不能放在栅栏接触的格子点上。一个给定的围栏可以容纳多少头奶牛?

 

思路:尝试了排除边界点,AC不了,应该是哪里边界没处理好,学长说还是皮克定律好写。

定理格点多边形面积=内点数+边界点数÷2-1 

a是内部点数量、 b是边界点数量 、s是三角形面积(铅锤高*水平 / 2,面积还是好求的)

pick:2s=2a+b-2   公式

怎么求边界点数?

(x1,y1)和(x2,y2)都再在一条线段上,

该线段上的格点数目为gcd( |x1-x2|,|y1-y2| )+ 1,

理解:对于横坐标差值和纵坐标差值求得的最大公因数g,相当于将横坐标差值分成g份,因为是整除,所以每份的左右端点都是整数,纵坐标也一样;因为是最大公因数,所以不可能再分更多份,因此 gcd( |x1-x2|,|y1-y2| )
即求得两端点间最多能分成多少段由格点分割的线段,再加上1即整条线段上的格点数目(端点数量,永远比线段数量多1)。
 

代码不长,皮克定理推了半天才勉强推出了,已经混乱了。。。

AC代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10,INF=0x3f3f3f3f;
//pick:2s=2a+b-2; < 皮克定理 >
//a内部点 b边界点 s多边形面积 
int n,m,p,x,y,ans;
int gcd(int a,int b){
    while(b^=a^=b^=a%=b);
    return a;
}
int main()
{
    cin>>n>>m>>p;
    double s=p*m*1.0/2;
    //gcd(线段的铅锤高,水平宽) = 线段的格点数-1
    if(n!=0)
        x=gcd(n,m)-1;        //线段上除2个端点外的格点数
    else if(n==0)
        x=m-1;
    if(p!=n)
        y=gcd(abs(n-p),m)-1;
    else if(p==n)
        y=m-1;
    ans=s-(x+y+p+2)/2+1;
    cout<<ans<<endl;
    return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值