题目链接: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. java_第一年_JavaWeb(5)

    HttpServletRequest对象 通过HttpServletRequest对象可获取客户端在访问服务器时,请求的所有信息 获取客户机的信息 getRequestURL:返回客户端发出请求时的完 ...

  2. stl(set和map)

    http://codeforces.com/gym/101911/problem/A Recently Monocarp got a job. His working day lasts exactl ...

  3. Qt 如何使用反射?

    Qt 如何使用反射? c++ 反射 标准库暂时还没有,那我们来看看如何使用 qt 来进行反射. 反射类的案例 1. 通过注册的类型需找 id 进行实例化该类 myclass.h #include &l ...

  4. k3 cloud套打模板中出现单元格数据为空的情况,及无法正确的选择数据源

    解决办法: 找到右边的导航树,点击数据表格选中,然后到左边点击右键,绑定数据

  5. 廖雪峰Python电子书总结

    函数 1.注意:函数的默认参数必须指向不可变对象 未修改前: def add_end(L=[]): L.append('END') return L 存在的问题:如果连续调用多次,会出现多个 'END ...

  6. 微信小程序(4)--二维码窗口

    微信小程序二维码窗口: <view class="btn" bindtap="powerDrawer" data-statu="open&quo ...

  7. USB接口外壳地和信号地间的处理

    USB外壳地和信号地之间串接1M电阻,并且还接一个0.01uf的电容到信号地,能否将一下这样处理的原理和目的: 1.将影响外壳的噪音滤除,不影响信号地: 2.迫使板子上电流是流入内部的信号地,而不是流 ...

  8. (解决某些疑难杂症)Ubuntu16.04 + NVIDIA显卡驱动 + cuda10 + cudnn 安装教程

    一.NVIDIA显卡驱动 打开终端,输入: sudo nautilus 在新打开的文件夹中,进入以下路径(不要用命令行): 左下角点计算机,lib,modules 这时会有几个文件夹,对每个文件夹都进 ...

  9. TCP和UDP的区别,以及它们对应的协议有哪些?

    TCP(传输控制协议) 是面向连接的协议.在收发数据前,一个TCP连接必须要经过“三次握手”建立可靠的连接.握手过程中传送的包里不包含数据,三次握手完毕后,客户端与服务器才正式开始传送数据.理想状态下 ...

  10. 吉首大学2019年程序设计竞赛(重现赛) B 干物妹小埋

    链接:https://ac.nowcoder.com/acm/contest/992/B来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K ...