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),(p,n),要求我们求出这个三角形包含的所有格点数量,(不包括边上的)

思路:

这道题使用皮克定理(适用于多边形、平面坐标系)

                                                            皮克定理:S= a+b/2-1 

其中 a指的是我们所要 求的坐标为整数的内部点数量,b指的是多边形边上的整数格点数量,s代表该多边形面积;

具体该怎么求解这个b呢?

一条边上的格点数是比如起点是(0,0) 终点是(n,m)则格点数就是gcd(n-0,m-0)+1,可以把0~n的横坐标和0~m的纵坐标平均分成gcd份,并且相邻都是整数,+1是因为间隔是gcd   但是点有gcd+1个。

但值得注意的是:在计算多边形边上的格点的时候可能会多加两条边连接的点导致答案错误。

因为这道题中是三角形所以会有三个顶点会被多算一次所以+3-3正好抵消不用gcd+1直接gcd就可

这道题的面积比较好求,S=\left ( p*m \right )/2

通过代码:

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define x first
#define y second
using namespace std;
int n,m,p;
int gcd(int a,int b)
{
	if(b==0) return a;
	return gcd(b,a%b);
}
void solve()
{
	cin>>n>>m>>p;
	int s=m*p/2;
	int edg=gcd(n,m)+gcd(abs(n-p),m)+p;
	int a=s+1-edg/2;
	cout<<a<<endl;
}
int main()
{
	IOS;
	int T;
	T=1;
	while(T--)
	{
		solve();
	}
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值