首页 > 其他分享 >hdu1705 Count the grid

hdu1705 Count the grid

时间:2024-10-02 16:22:50浏览次数:1  
标签:Count gcd int hdu1705 abs grid y1 x1 Math

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

多边形边界上的整数点怎么求呢?

当然是gcd啦~~  gcd(x1-x2, y1-y2)就是这条边上整数点的个数。但是仅仅一条边是不准确的(有一个端点没有算上),需要把所有边的gcd加上才是皮克定理中的「b」。

1.0*gcd(Math.abs(x1-x2),Math.abs(y1-y2))+gcd(Math.abs(x1-x3),Math.abs(y1-y3))+gcd(Math.abs(x2-x3),Math.abs(y2-y3));//计算边点
1.0*Math.abs((x2-x1)*(y3-y1)-(y2-y1)*(x3-x1))/2;//由三点坐标求三角形面积,用了向量叉积

 

package javahd;

//import java.text.DecimalFormat;
import java.util.Scanner;

public class hdu1705 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //DecimalFormat df = new DecimalFormat("0.000");
        while (sc.hasNext()){
            int x1 = sc.nextInt();
            int y1 = sc.nextInt();
            int x2 = sc.nextInt();
            int y2 = sc.nextInt();
            int x3 = sc.nextInt();
            int y3 = sc.nextInt();
            if (x1==0 && x2==0 && x3==0 && y1==0 && y2==0 && y3==0){
                break;
            }
            double edgnod=1.0*gcd(Math.abs(x1-x2),Math.abs(y1-y2))+gcd(Math.abs(x1-x3),Math.abs(y1-y3))+gcd(Math.abs(x2-x3),Math.abs(y2-y3));//计算边点我也不知道这是啥原理
            double s=1.0*Math.abs((x2-x1)*(y3-y1)-(y2-y1)*(x3-x1))/2;//由三点坐标求三角形面积,用了向量叉积
            //System.out.println(df.format(s-edgnod/2+1));
            int res = (int) (s-edgnod/2+1);
            System.out.println(res);
        }
    }
    public static int gcd(int a ,int b){
        if (b>a){
            int tmp = a;
            a = b;
            b = tmp;
        }
        if (b==0){
            return a;
        }else{
            return gcd(b,a%b);
        }
    }
}

 

标签:Count,gcd,int,hdu1705,abs,grid,y1,x1,Math
From: https://www.cnblogs.com/xiaohuangTX/p/18444850

相关文章

  • 【VBA】UsedRangeの範囲から最終行など取得【UsedRange.Rows.Countなど】
    参考元:【VBA】UsedRangeの範囲から最終行など取得【UsedRange.Rows.Countなど】https://daitaideit.com/vba-usedrange/ポイントとなるVBAコードWithActiveSheet.UsedRange.Select'使用しているセル範囲'行.Rows(1).Select'1行目.Rows(.Rows.C......
  • P4778 Counting Swaps
    题意:给定一个\(1\simn\)的排列\(a\)。每次可以选两个位置\(i,j\),耗费\(1\)的代价交换\(a_i,a_j\)。问使得\(a\)升序排列的最小代价是多少,以及方案数。\(1\len\le10^5\)。求最小代价:连边\(i\rightarrowa_i\),得到若干个环。一个大小为\(x\)的环需要\(x-1\)次操作......
  • VMware Tanzu Kubernetes Grid Integrated Edition (TKGI) 1.20.0 - 运营商 Kubernete
    VMwareTanzuKubernetesGridIntegratedEdition(TKGI)1.20.0-运营商Kubernetes解决方案Kubernetes-basedcontainersolutionwithadvancednetworking,aprivatecontainerregistry,andlifecyclemanagement请访问原文链接:https://sysin.org/blog/vmware-tkgi/,......
  • VMware Tanzu Kubernetes Grid Integrated Edition 1.20 发布下载,新增功能概览
    VMwareTanzuKubernetesGridIntegratedEdition1.20发布下载,新增功能概览VMwareTanzuKubernetesGridIntegratedEdition(TKGI)1.20.0-运营商Kubernetes解决方案Kubernetes-basedcontainersolutionwithadvancednetworking,aprivatecontainerregistry,an......
  • sumifs countifsにて記載した複数条件がAnd関係である
    SUMIFS函数可用于对满足多个条件的范围进行求和。函数公式:=SUMIFS(求和范围,条件范围1,条件1,[条件范围2,...],[条件2,...])COUNTIFS関数の使い方ANDだけでなくOR条件も指定できる=COUNTIFS(範囲1,検索条件1,範囲2,検索条件2,...)書き方:......
  • sumifs countifsにて記載した複数条件がAnd関係である
    sumifscountifsにて記載した複数条件がAnd関係であるCOUNTIF関数=COUNTIF(範囲,条件)「範囲」の中の「条件」に合う「セルの個数」を求めます。例:=COUNTIF($C$4:$C$14,H4)結果:xxxxSUMIF関数=SUMIF(範囲,条件,合計する範囲)例:=SUMIF($C$4:$C$14,H4,$F$4:$F$14)結......
  • BFA507 Accounting and Accountability for Decision
    BFA507AccountingandAccountabilityforDecisionMaking-Sem2,2024AssessmentTask2:OralpresentationDue: Week10-Friday,4thOctober2024at5.00pmILOsAddressed: ILO1,ILO2Maximumlength/format: 5-minutevideopresentationincluding:Powerpo......
  • vue3 vxe-grid 通过数据库返回的列信息,生成columns,并且其中有一列是img类型,进行slots
    1、一般我们写死的列信息的时候,会这样定义:2、然后我们在template里面,这样这样写slots格式化部分:这样表格中就会展示出一张图片,并且,我们点击了可以查看大图。3、那么我们从数据库中返回的列,应该如何去写:letfields={field:item.fieldname,......
  • Building Accounting Information System using MS Access
    DatabaseAssignment(Fall2024)BuildingAccountingInformationSystemusingMSAccess(100marks)allaccounts’beginningbalancesarezeroSPELimitedsellsdifferentkindsofsmartphonesthatitpurchasesfromdifferentmanufacturers.Itscustomer......
  • Flink(二)搭建Maven工程实现WordCount
    开发环境编写WordCountpom文件<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation=&qu......