TZOJ 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

题意:

有一个位于第一象限的三角形,其中一个点为原点(0,0),另外一个点位于x轴上的(p,0),剩下一个点位于(n,m)。

求这个三角形内的格点有多少个(不包括三角形的边界)

思路:

(1) 使用皮克定理可以轻松解决!

皮克定理是指一个计算点阵中顶点在格点上的多边形面积公式,该公式可以表示为2S=2a+b-2,其中a表示多边形内部的点数b表示多边形边界上的点数S表示多边形的面积

(2) gcd(线段的铅锤高,水平宽) = 线段的格点数-1

#include<bits/stdc++.h> 
using namespace std;
//pick:2s=2a+b-2;
//a内部点 b边界点 s多边形面积
int main()
{
    int n,m,p,b1,b2,a;
    double s;
    cin>>n>>m>>p;
    s=p*m*1.0/2;
    if(n!=0)
        b1=__gcd(n,m)-1;        //线段上除2个端点外的格点数
    else if(n==0)b1=m-1;
    
    if(p!=n)
        b2=__gcd(abs(n-p),m)-1;
    else if(p==n)b2=m-1;

    a=s-(b1+b2+p+2)/2+1;
    cout<<a<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/kannyi/p/9584063.html