皮克定理(upc组队训练赛 Water Testing)

Water Testing

时间限制: 1 Sec 内存限制: 128 MB

题目描述

You just bought a large piece of agricultural land, but you noticed that – according to regulations – you have to test the ground water at specific points on your property once a year. Luckily the description of these points is rather simple. The whole country has been mapped using a Cartesian Coordinate System (where (0, 0) is the location of the Greenwich Observatory). The corners of all land properties are located at integer coordinates according to this coordinate system. Test points for ground water have to be erected on every point inside a property whose coordinates are integers.

输入

The input consists of:
• one line with a single integer n (3 ≤ n ≤ 100 000), the number of corner points of your property;
• n lines each containing two integers x and y (−106 ≤ x, y ≤ 106 ), the coordinates of each corner.
The corners are ordered as they appear on the border of your property and the polygon described by the points does not intersect itself.

输出

The number of points with integer coordinates that are strictly inside your property.

样例输入

4
0 0
0 10
10 10
10 0

样例输出

81

学习皮克定理,对于一个封闭图形,在他内部整数点的个数关系:

S = a + b/2 -1(其中,S为图形面积,a为内部点数,b为在边上的点数)

下面是ac代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <queue>
#include <map>
#include <vector>
#define ll long long
using namespace std;
const int N = 1e6+6;
struct Node
{
    ll x, y;
}su[N];
int n;
ll gcd(ll a, ll b)
{
    return b?gcd(b,a%b):a;
}
ll num(Node a, Node b)
{
    if (a.x == b.x &&a.y == b.y) return 0;
    if (a.x == b.x) return abs(a.y - b.y);
    if (a.y == b.y) return abs(a.x - b.x);
    return gcd(abs(a.x - b.x), abs(a.y - b.y));
}
double cal(Node a, Node b)
{
    return (a.x*b.y - a.y * b.x);
}
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%lld%lld", &su[i].x, &su[i].y);
    double sans = su[n].x*su[1].y - su[n].y*su[1].x;
        for(int i=1;i<=n;++i)
            sans += su[i].x*su[i+1].y - su[i].y*su[i+1].x;
    sans = abs(sans)*1.0/2.0;
    su[0].x = su[0].y = 0;
    ll ans1 = 0;
    for (int i = 1; i < n; i++)
    {
        ans1 += num(su[i], su[i+1]);
    }
    ans1 += num(su[n], su[1]);
  //  cout << sans << endl;
   // cout << ans1 << endl;
    ll ans = sans - ans1 / 2 + 1;
    printf("%lld\n", ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值