题目:
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),要求我们求出这个三角形包含的所有格点数量,(不包括边上的)
思路:
这道题使用皮克定理(适用于多边形、平面坐标系)
皮克定理:
其中 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就可
这道题的面积比较好求,
通过代码:
#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();
}
}