题干描述
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;
}