TZOJ5103: Electric Fence通电围栏 (皮克定理)

TZOJ5103: Electric Fence通电围栏

题目传送门

描述

In this problem, “lattice points” in the plane are points with integer coordinates.

In order to contain his cows, Farmer John constructs a triangular electric fence by stringing a “hot” wire from the origin (0,0) to a lattice point [n,m] (0<=n<32,000, 0<m<32,000), then to a lattice point on the positive x axis [p,0] (0<p<32,000), and then back to the origin (0,0).

A cow can be placed at each lattice point within the fence without touching the fence (very thin cows). Cows can not be placed on lattice points that the fence touches. How many cows can a given fence hold?

输入

The single input line contains three space-separated integers that denote n, m, and p.

输出

A single line with a single integer that represents the number of cows the specified fence can hold.

样例输入
7 5 10
样例输出
20
解题思路

题目给出了三个点(0,0),(n,m)和(p,0),他们组成一个三角形围栏,求该三角形围栏内部(不包含边上)整数点(奶牛)的最大数目。

那么该题很明显是考察的是皮克定理:皮克定理是指一个计算点阵中顶点在格点上的多边形面积公式,该公式可以表示为S=a+b÷2-1,其中a表示多边形内部的点数,b表示多边形落在格点边界上的点数,S表示多边形的面积。

那么得到a = S - b / 2 + 1,S可以使用(三个点的叉积 / 2)求解,b可以使用欧几里得算法(辗转相除法得到),即b = gcd(abs(x1 - x2), abs(y1 - y2))。证明可以参考gcd求直线上的整点数目证明

那么我们枚举p,求解最大内部整点数即可。其实也可以不枚举p直接利用gcd求解三边点数再将其求和得出结果:三角形边(0, 0) → (n, m)上的格点数为gcd(n, m),三角形边(n, m) → (p, 0)上的格点数为gcd(abs(n - p), m),三角形边(p, 0) → (0, 0)上的格点数为p,则b = gcd(n, m) + gcd(abs(n - p), m) + p

代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define IOS ios::sync_with_stdio(0),cin.tie(0)
struct info {
    ll x, y;
};
double xmult(info a, info b, info c) {
    return (b.x - c.x) * (a.y - c.y) - (b.y - c.y) * (a.x - c.x);
}
ll gcd(ll a, ll b) {return b? gcd(b,a%b):a;}
void solve() {
    ll n, m, p;cin >> n >> m >> p;
    vector<info> a(3);
    a[1] = {n, m};
    ll mx = -1;
    for (int i = 1; i <= p; ++i) {
        a[2].x = i;
        ll ct = 0;
        for (int j = 0; j < 3; ++j) {
            ll x = a[j].x - a[(j + 1) % 3].x;
            ll y = a[j].y - a[(j + 1) % 3].y;
            ct += gcd(abs(x), abs(y));
        }
        double s = abs(xmult(a[0], a[1], a[2]));
        double ans = s / 2 - ct / 2.0 + 1;
        mx = max(mx, (ll)(ans));
    }
    cout << mx << endl;
}
int main() {
    IOS;
//    ll t; cin >> t;
    ll t = 1;
    while (t--) solve();
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define IOS ios::sync_with_stdio(0),cin.tie(0)
struct info {
    ll x, y;
};
double xmult(info a, info b, info c) {
    return (b.x - c.x) * (a.y - c.y) - (b.y - c.y) * (a.x - c.x);
}
ll gcd(ll a, ll b) {return b? gcd(b,a%b):a;}
void solve() {
    ll n, m, p;cin >> n >> m >> p;
    vector<info> a(3);
    a[1] = {n, m};
    a[2] = {p, 0};
    double s = abs(xmult(a[0], a[1], a[2])) / 2;
    ll b = gcd(n, m) + gcd(abs(n - p), m) + p;
    ll ans = (ll)(s - b / 2.0 + 1);
    cout << ans << endl;
}
int main() {
    IOS;
//    ll t; cin >> t;
    ll t = 1;
    while (t--) solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值