Electric Fence

发布时间 2023-08-12 18:44:40作者: Minza

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代码

#include <bits/stdc++.h>
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();
    }
}