题目链接: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. idea2019.1 永久破解 亲测可用

    idea2019突然注册码突然失效了,搜了很多破解办法,这个还是有效的:https://www.jianshu.com/p/b6dd43618a66

  2. 2019-9-2-win10-UWP-MvvmLight入门

    title author date CreateTime categories win10 UWP MvvmLight入门 lindexi 2019-09-02 12:57:38 +0800 2018 ...

  3. AES加密的C语言实现

    摘自网上一种AES加密,用C语言实现通过32字节密钥对16字节长度数据进行加密. #include <string.h> #include <stdio.h> #ifndef ...

  4. 二、TortoiseSVN 合并、打分支、合并分支、切换分支

    一.合并 点击Edit conflict来编辑冲突: 在合并后的枝干对应栏中编辑后,Save保存后关闭. 二.TortoiseSVN 打分支.合并分支.切换分支 1.SVN打分支 方式一:先检出,再打 ...

  5. Java虚拟机——Java内存区域与内存溢出

    内存区域 Java虚拟机在执行Java程序的过程中会把他所管理的内存划分为若干个不同的数据区域.Java虚拟机规范将JVM所管理的内存分为以下几个运行时数据区:程序计数器.Java虚拟机栈.本地方法栈 ...

  6. win10 护眼

  7. 前端学习(三十七)angular(笔记)

    MVC     后台    M         Module             数据层    V         View             视图层    C         Contro ...

  8. javascript数组赋值操作的坑

    描述:数组对象赋值,即arr1=[{},{},{}]这种数据结构的对象赋值,将arr1赋值给arr2:然后删除arr2里的元素 一.最常用的= arr2 = arr1; detect(val) { l ...

  9. 【学习笔记】虚树复习记(BZOJ2286 SDOI2011 消耗战)

    想写战略游戏却想不起来虚树T^T 所以就有了这篇复习记QwQ ——简介!—— 我们在处理树上问题的时候,dfs是一个常用手段,但是我们发现,如果一棵树上只有一部分关键点,每次dfs需要访问好多不是关键 ...

  10. Java中避免空指针常见的方法

    什么是空指针? 当一个变量的值为null时,在Java里表示一个不存在的空对象,没有实际内容,没有给它分配内存,null也是对象成员的默认值.所以,一个对象如果没有进行初始化操作,这时如果调用这个对象 ...