poj 2954 Triangle

题意

给出一个三角形的三个点,问三角形内部有多少个整点。

解法

pick's law

一个多边形如果每个顶点都由整点构成,该多边形的面积为\(S\),该多边形边上的整点为\(L\),内部的整点为\(N\),则有:

$ N + L/2 - 1 = S \(
而对于两个点\)A(x_1,y_1)\(与\)B(x_2,y_2)\(,其边内部的整点数为:(不包含端点)
\) gcd ( abs(x_1 - x_2) , abs(y_1 - y_2) ) - 1 $

代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#define INF 2139062143
#define MAX 0x7ffffffffffffff
#define del(a,b) memset(a,b,sizeof(a))
#define Rint register int
using namespace std;
typedef long long ll;
template<typename T>
inline void read(T&x)
{
x=0;T k=1;char c=getchar();
while(!isdigit(c)){if(c=='-')k=-1;c=getchar();}
while(isdigit(c)){x=x*10+c-'0';c=getchar();}x*=k;
}
struct G{
double x,y;
G(double x=0.0,double y=0.0):x(x),y(y){}
};
G operator + (const G &a ,const G& b){return G(a.x+b.x,a.y+b.y);}
G operator - (const G &a ,const G& b){return G(a.x-b.x,a.y-b.y);}
double cross(G a,G b){
return fabs(a.x*b.y-a.y*b.x);
}
struct node{
int x,y;
};
node a[3];
int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
int get_node(node a,node b){
double x=fabs((double)a.x-b.x),y=fabs((double)a.y-b.y);
if(x==0&&y==0) return 0;
return gcd(x,y)-1;
}
int main()
{
while(1){
bool f=0;
for(int i=0;i<3;i++){
read(a[i].x),read(a[i].y);
if(a[i].x || a[i].y) f=1;
}
if(!f) break;
G b=G(a[1].x-a[0].x,a[1].y-a[0].y),c=G(a[2].x-a[0].x,a[2].y-a[0].y);
int s=int(cross(b,c)/2);
int L=get_node(a[0],a[1])+get_node(a[0],a[2])+get_node(a[2],a[1])+3;
printf("%d\n",s+1-L/2);
}
return 0;
}

poj 2954 Triangle 三角形内的整点数的更多相关文章

  1. POJ 1265 Area POJ 2954 Triangle Pick定理

    Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5227   Accepted: 2342 Description ...

  2. poj 2954 Triangle(Pick定理)

    链接:http://poj.org/problem?id=2954 Triangle Time Limit: 1000MS   Memory Limit: 65536K Total Submissio ...

  3. poj 2954 Triangle

    pick公式+gcd公式 #include<iostream> #include<map> #include<string> #include<cstring ...

  4. POJ 2954 Triangle (pick 定理)

    题目大意:给出三个点的坐标,问在这三个点坐标里面的整数坐标点有多少个(不包含边上的) 匹克定理:I = (A-E) / 2 + 1; A: 表示多边形面积 I : 表示多边形内部的点的个数 E: 表示 ...

  5. POJ 2954 /// 皮克定理+叉积求三角形面积

    题目大意: 给定三角形的三点坐标 判断在其内部包含多少个整点 题解及讲解 皮克定理 多边形面积s = 其内部整点in + 其边上整点li / 2 - 1 那么求内部整点就是 in = s + 1 - ...

  6. poj 1265&&poj 2954(Pick定理)

    Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5811   Accepted: 2589 Description ...

  7. 2D空间中判断一点是否在三角形内

    要注意如果是XY坐标轴的2D空间,要取差乘分量z而不是y. 实现原理是,将三角形ABC三个边(AB,BC,CA)分别与比较点判断差乘,如果这3个差乘结果表示的方向一致,说明就在三角形内. 效果: 代码 ...

  8. hrbustoj 1142:围困(计算几何基础题,判断点是否在三角形内)

    围困 Time Limit: 1000 MS     Memory Limit: 65536 K Total Submit: 360(138 users) Total Accepted: 157(12 ...

  9. 【Leetcode】判断平面中1个点是否落在三角形内

    参考资料: 题目: https://blog.csdn.net/dongtinghong/article/details/78657403 符号重载: https://blog.csdn.net/cd ...

随机推荐

  1. POJ 1061 青蛙的约会( 拓欧经典题 )

    链接:传送门 思路:简单拓展欧几里德,分析后可以得到方程 x + m * t = y + n * t + L * s( s控制圈数,t代表跳t次会碰面 ),经过化简可以得到 ( n - m ) * t ...

  2. 03.IO读写-1.IO介绍

    1 文件操作介绍 in: 输入,读入.从硬盘中读到内存 out: 输出.从内存写到硬盘 文件的作用: 数据存储 2 文件的打开与关闭 2.1 打开文件 在Python,使用open函数,可以打开一个已 ...

  3. Python 绘制2016世界GDP地图

    2016世界GDP地图 从https://datahub.io/core/gdp#data下载得到json文件. # country_code.py 获取国家二字代码 # 从pygal.maps.wo ...

  4. js中获取宽高

    <script type="text/javascript"> function getWH() { var a = ""; a += " ...

  5. 【CodeForces 987C】Three displays

    [链接] 我是链接,点我呀:) [题意] [题解] 动态规划 设dp[i][j]表示前i个数字,选了j个的最小花费. dp[i][j] = min(dp[k][j-1]+b[i]);//其中a[i]& ...

  6. hdu 3836 强连通+缩点:加边构强连通

    #include<stdio.h>//求出其所有的强连通分量缩点,选出出度和入度最大的那个就是要求的边 #include<string.h> #include<stdli ...

  7. java cocurrent包

    1. java.util.concurrent - Java 并发工具包 Java 5 添加了一个新的包到 Java 平台,java.util.concurrent 包.这个包包含有一系列能够让 Ja ...

  8. thrift 版本不一致导致 @Override 报错

    thrift 版本不一致导致 @Override 报错 学习了:http://blog.csdn.net/antony1776/article/details/78920888 版本不一致导致的: 在 ...

  9. 【LeetCode-面试算法经典-Java实现】【056-Merge Intervals(区间合并)】

    [056-Merge Intervals(区间合并)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a collection of intervals, ...

  10. unity3d 中动画的帧事件

    代码事件监听 using UnityEngine; using System.Collections; public class BoxEventScript : MonoBehaviour { vo ...