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

题目分析

这题可以根据皮克定理处理,皮克定理:多边形面积=多边形内部的格点数+多边形边上格点二分之一-1,本题的多边形为三角形,所以内格点数即为答案。因为一条边固定在x轴,所以要求出另外两条边之间的格点,可以遍历1到m-1的y值,将其代入两条边的方程中,求出每个y值对应的左右端值,相减求出每行的格点数。

#include<bits/stdc++.h>
const int N=2e5+5;
using namespace std;
int n,m,p,sum=0;

int main()
{
	double a,b;
	cin>>n>>m>>p;
	for(int i=1;i<m;i++)
	{
		a=(i*n/m*1.0);
		if(a==ceil(a)) a+=1;
		else a=ceil(a);
		b=(i*1.0*(n-p)/m+p);
		if(b==floor(b)) b-=1;
		else b=floor(b);
		if(b-a+1>0) sum+=b-a+1;
	}
	cout<<sum<<endl;
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值