题目链接:LightOJ 1418

Problem Description

I have bought an island where I want to plant trees in rows and columns. So, the trees will form a rectangular grid and each of them can be thought of having integer coordinates by taking a suitable grid point as the origin.

But, the problem is that the island itself is not rectangular. So, I have identified a simple polygonal area inside the island with vertices on the grid points and have decided to plant trees on grid points lying strictly inside the polygon.

Figure: A sample of my island

For example, in the above figure, the green circles form the polygon, and the blue circles show the position of the trees.

Now, I seek your help for calculating the number of trees that can be planted on my island.

Input

Input starts with an integer \(T (≤ 100)\), denoting the number of test cases.

Each case starts with a line containing an integer \(N (3 ≤ N ≤ 10000)\) denoting the number of vertices of the polygon.

Each of the next \(N\) lines contains two integers \(x_i y_i (-10^6 ≤ x_i, y_i ≤ 10^6)\) denoting the co-ordinate of a vertex. The vertices will be given in clockwise or anti-clockwise order. And they will form a simple polygon.

Output

For each case, print the case number and the total number of trees that can be planted inside the polygon.

Sample Input

1

9

1 2

2 1

4 1

4 3

6 2

6 4

4 5

1 5

2 3

Sample Output

Case 1: 8

Note

Dataset is huge, use faster I/O methods.

Solution

题意:

给定一个多边形,顶点都在格点上,求多边形内部的格点个数。

思路

Pick 定理 裸题。

#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef double db;
const db eps = 1e-10;
const db pi = acos(-1.0);
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll maxn = 1e5 + 10; inline int dcmp(db x) {
if(fabs(x) < eps) return 0;
return x > 0? 1: -1;
} class Point {
public:
ll x, y;
Point(ll x = 0, ll y = 0) : x(x), y(y) {}
void input() {
scanf("%lld%lld", &x, &y);
}
Point operator-(const Point a) {
return Point(x - a.x, y - a.y);
}
ll cross(const Point a) {
return x * a.y - y * a.x;
}
}; Point p[maxn]; ll gcd(ll a, ll b) {
return b == 0? a: gcd(b, a % b);
} int main() {
int T;
scanf("%d", &T);
for(int _ = 1; _ <= T; ++_) {
int n;
scanf("%d", &n);
ll on = 0;
ll s = 0;
for(int i = 0; i < n; ++i) {
p[i].input();
}
p[n] = p[0];
for(int i = 0; i < n; ++i) {
s += (p[i + 1] - p[0]).cross(p[i] - p[0]);
on += gcd(abs(p[i].x - p[i + 1].x), abs(p[i].y - p[i + 1].y));
}
s = abs(s);
ll in = s / 2 - on / 2 + 1;
printf("Case %d: ", _);
printf("%lld\n", in);
}
return 0;
}

LightOJ 1418 Trees on My Island (Pick定理)的更多相关文章

  1. UVa 10088 - Trees on My Island (pick定理)

    样例: 输入:123 16 39 28 49 69 98 96 55 84 43 51 3121000 10002000 10004000 20006000 10008000 30008000 800 ...

  2. UVa 10088 (Pick定理) Trees on My Island

    这种1A的感觉真好 #include <cstdio> #include <vector> #include <cmath> using namespace std ...

  3. HDU 3775 Chain Code ——(Pick定理)

    Pick定理运用在整点围城的面积,有以下公式:S围 = S内(线内部的整点个数)+ S线(线上整点的个数)/2 - 1.在这题上,我们可以用叉乘计算S围,题意要求的答案应该是S内+S线.那么我们进行推 ...

  4. 【POJ】2954 Triangle(pick定理)

    http://poj.org/problem?id=2954 表示我交了20+次... 为什么呢?因为多组数据我是这样判断的:da=sum{a[i].x+a[i].y},然后!da就表示没有数据了QA ...

  5. Area(Pick定理POJ1256)

    Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5429   Accepted: 2436 Description ...

  6. poj 2954 Triangle(Pick定理)

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

  7. poj 1265 Area (Pick定理+求面积)

    链接:http://poj.org/problem?id=1265 Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:  ...

  8. poj1265Area(pick定理)

    链接  Pick定理是说,在一个平面直角坐标系内,如果一个多边形的顶点全都在格点上,那么这个图形的面积恰好就等于边界上经过的格点数的一半加上内部所含格点数再减一. pick定理的一些应用 题意不好懂, ...

  9. pick定理:面积=内部整数点数+边上整数点数/2-1

    //pick定理:面积=内部整数点数+边上整数点数/2-1 // POJ 2954 #include <iostream> #include <cstdio> #include ...

随机推荐

  1. indexOf与includes的区别

    indexOf与includes的区别:https://blog.csdn.net/gtLBTNq9mr3/article/details/78700118 includes和indexOf相比较:相 ...

  2. jQuery easyUI 使用 datagrid 表格

    获取后台数据依旧是使用一般处理程序(ashx) ,分页上添加一个函数(pagerFilter(data)) 前端代码: <%@ Page Language="C#" Auto ...

  3. vue iview分页

    距离上次博客更新已经快一个月了,期间也有想法在空闲的时候更新几篇博文. 燃鹅,最近懒癌作祟,丢掉的东西越来越多,再不遏止的话就真成癌了. 趁着刚看完一篇心灵鸡汤,让打满鸡血的我总结下前段时间用到的iv ...

  4. tensorflow创建cnn网络进行中文手写文字识别

    数据集下载地址:http://www.nlpr.ia.ac.cn/databases/handwriting/download.html chinese_write_detection.py # -* ...

  5. 2019-1-5-Windows-的-Pen-协议

    title author date CreateTime categories Windows 的 Pen 协议 lindexi 2019-01-05 11:14:49 +0800 2019-01-0 ...

  6. 125-FMC125-两路125Msps AD,两路160Msps DA FMC子卡模块

    FMC125-两路125Msps AD,两路160Msps DA FMC子卡模块 1.板卡概述  该板卡可实现2路14bit 250Msps AD 和2路16bit 160MspsDA功能,FMC连接 ...

  7. MySQL--15 MHA简介

    目录 一.MHA简介 二.工作流程 三.MHA架构图 四.MHA工具介绍 五.基于GTID的主从复制 六.部署MHA 一.MHA简介 松信嘉範: MySQL/Linux专家 2001年索尼公司入职 ...

  8. RPC_简洁版

    1.定义服务接口 public interface RpcService { int printHello(int msg); } 2.定义服务接口实现类 public class RpcInterf ...

  9. javascript中数组元素删除方法splice,用在for循环中巨坑

    一.demo splice: 该方法会改变自动原始数组长度 实例: var array = ["aa","dd","cc","aa ...

  10. IO流 读写文件

    读写文件 如前所述,一个流被定义为一个数据序列.输入流用于从源读取数据,输出流用于向目标写数据. 下图是一个描述输入流和输出流的类层次图. 下面将要讨论的两个重要的流是 FileInputStream ...