Electric Fence
5103: Electric Fence
时间限制(普通/Java):1000MS/3000MS 内存限制:64000KByte
描述
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
思路
用 皮克定理秒了
皮克定理:一个计算点阵中顶点在格点上的多边形面积公式,该公式可以表示为2S=2a+b-2
S表示多边形的面积
a表示多边形内部的点数
b表示多边形边界上的点数
线段的两个顶点:(x1,y1) (x2, y2);
该条线段上的点为 gcd(|x1-x2|,|y1-y2|)+1;
则多边形边界上的点数量 b 由每条边上的点之和再减去边数量即可
AC代码
复制代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
using namespace std;
int gcd(int a,int b)
{
if(b)return gcd(b,a%b);
return a;
}
void solve()
{
int n,m,p;
cin>>n>>m>>p;
double s=1.0*p*m/2;
int b=gcd(n,m)+gcd(abs(n-p),m)+p;
int ans=(2*s-b+2)/2;
cout<<ans<<endl;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _=1;
// cin>>_;
while(_--)
{
solve();
}
}
本文作者:Minza
本文链接:https://www.cnblogs.com/minz-io/p/17625249.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步