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

        tzoj的一道题目,不知道哪里整过来的,实际上考的就是皮克定理,皮克定理的定义是:格点多边形面积=内点数+边界点数÷2-1。
        这题相当于求内点数,变换一下,内点数=格点多边形面积-边界点数÷2+1,面积就是下底乘高除2,边界点数( 对于两端点(x1,y1),(x2,y2)都再格点上的一条线段,该线段上的格点数目为gcd( |x1-x2|,|y1-y2| )+ 1)可以用gcd求得,其实本来我觉得这样写少了三个点的,但是后来发现点还是被加进去了,所以这样是对的。

        后面是代码:

#include<bits/stdc++.h>
#define ll long long
#define N 50005
#define M 105
#define debug cout<<"~~debug "
#define rc p<<1|1
#define lc p<<1
#define PII pair<long long,long long>
#define pb push_back
#define Inf 0x3f3f3f3f
//neuro-sama
const ll mod = 1e9+7;
using namespace std;
inline int read()
{
	char c=getchar();int f=1,x=0;
	while(c>'9'||c<'0'){ if(c=='-') f=-f;c=getchar();}
	while(c<='9'&&c>='0'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
	return f*x;
}
int T;
ll gcd(ll a,ll b){
	return b==0? a:gcd(b,a%b);
}
void solve()
{
	ll n,m,p;
	cin>>n>>m>>p;
	//皮克定理 格点多边形面积=内点数+边界点数/2-1
	//S=a+b/2-1; a=S-b/2+1;	
	cout<<(((m*p-gcd(n,m)-p-gcd(abs(p-n),m))>>1)+1)<<endl;
}
int main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	T=1;
//	cin>>T;
	while(T--)
	{
		solve();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值