5103: Electric Fence

描述

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

思路:
皮克定理:一个计算点阵中顶点在格点上的多边形面积公式,该公式可以表示为S=a+b/2-1
求出三条边上的坐标点数量b及面积S,即可推出a
AC代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<PII, int> PPI;
#define io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define FOR(i,s,e)for(int i=(s);i<=(e);i++)
#define mem(a, b) memset((a), (b), sizeof(a))
#define inf 0x3f3f3f3f
#define seed 13331
#define MOD1 1000000007
#define MOD2 1000000009
#define MOD3 998244353
const int N = 2e5+7;
long long read(){
    char ch=getchar();long long nn=0,ssss=1;
    while(ch<'0'||ch>'9'){if(ch=='-')ssss*=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){nn=nn*10+(ch-'0');ch=getchar();}
    return nn*ssss;
}
int gcd(int a,int b)
{
	return b ? gcd(b, a % b) : a;
}
void solve(int i_)
{       
	int n,m,p;
	cin>>n>>m>>p;
	cout<<(p*m-gcd(abs(n-p),m)-gcd(n,m)-p+2)/2;
}
int main()
{
    io;
    int T=1,i_;
    // cin>>T;
    // for(i_=1;i_<=T;i_++)
    solve(i_);

    return 0;
}
posted @ 2023-08-13 02:53  holy_crap  阅读(11)  评论(0编辑  收藏  举报