描述
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),(p, 0),(n, m)三个点围成的三角形内部有几个不在边界上的整数点。
令a = (0, 0), b = (p, 0), c = (n, m)
因为有一边固定在x轴上,一行一行平行于x轴找比较方便,于是遍历纵坐标从1到m - 1,对于每个整数y分别求在直线ac和bc之间的点的个数之和就是答案。可以直接得到ac和bc的方程然后将y带入求解得到两端的值,分别上取整和下取整相减就是这一行在三角形中的点的个数,注意,边界上的点不包括,所以如果算出来的边界有整数还要再加一或者减一。
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 110, MOD = (1 << 31) - 1, INF = 1e8;
LL n, m, p;
double a, b;
LL f[N][N];
int main() {
ios::sync_with_stdio(false);
cin >> n >> m >> p;
LL ans = 0;
for(int i = 1;i < m;i ++) {
a = (i * n / (double) m);
if(a == ceil(a)) a += 1;
else a = ceil(a);
if(n != p) b = (i * (double) (n - p) / m + (double)p);
else b = n;
if(b == floor(b)) b -= 1;
else b = floor(b);
ans += (LL)max((double)0, b - a + 1);
}
cout << ans << '\n';
return 0;
}