zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    HDU 2089 不要62
    HDU 5038 Grade(分级)
    FZU 2105 Digits Count(位数计算)
    FZU 2218 Simple String Problem(简单字符串问题)
    FZU 2221 RunningMan(跑男)
    FZU 2216 The Longest Straight(最长直道)
    FZU 2212 Super Mobile Charger(超级充电宝)
    FZU 2219 StarCraft(星际争霸)
    FZU 2213 Common Tangents(公切线)
    FZU 2215 Simple Polynomial Problem(简单多项式问题)
  • 原文地址:https://www.cnblogs.com/kannyi/p/9584063.html
Copyright © 2011-2022 走看看