当前位置: 代码迷 >> 综合 >> Hust oj 1056 Electric Fence(皮克定理)
  详细解决方案

Hust oj 1056 Electric Fence(皮克定理)

热度:55   发布时间:2023-12-22 04:39:46.0
Electric Fence
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 111(50 users) Total Accepted: 48(40 users) Rating:  Special Judge: No
Description

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<32000, 0<m<32000), then to a lattice point on the positive x axis [p,0] (p>0), 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?

Input

First line contains an integer n: the number of test case.

Then n lines follow, each line contains three space-separated integers that denote n, m, and p.

Output

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

Sample Input
2
7 5 10
1 1 2
Sample Output
20

0

这题我会了皮克定理才会的,不会之前一点思路没有

一张方格纸上,上面画着纵横两组平行线,相邻平行线之间的距离都相等,这样两组平行线的交点,就是所谓格点。如果取一个格点做原点O,如图1,取通过这个格点的横向和纵向两直线分别做横坐标轴OX和纵坐标轴OY,并取原来方格边长做单位长,建立一个坐标系。这时前面所说的格点,显然就是纵横两坐标都是整数的那些点。如图1中的O、P、Q、M、N都是格点。由于这个缘故,我们又叫格点为整点。
一个多边形的顶点如果全是格点,这多边形就叫做格点多边形。有趣的是,这种格点多边形的面积计算起来很方便,只要数一下图形边线上的点的数目及图内的点的数目,就可用公式算出。
这个公式是皮克(Pick)在1899年给出的,被称为“皮克定理”,这是一个实用而有趣的定理。给定顶点坐标均是整点(或正方形格点)的简单多边形, a=39,b=14,s=45,皮克定理说明了其面积S和内部格点数目a、边上格点数目b的关系:S=a+b÷2-1。(其中a表示多边形内部的点数,b表示多边形边界上的点数,S表示多边形的面积)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
using namespace std;int gcd(int x,int y)
{if(x < y){int temp = x;x = y;y = temp;}while(y){int temp = x % y;x = y;y = temp;}return x;
}int n,m,p;
int t;int main()
{scanf("%d",&t);while(t--){scanf("%d %d %d",&n,&m,&p);int sum;int gcd1 = gcd(n,m);int gcd2 = gcd(fabs(p-n),m);sum = gcd1 + gcd2 + p;int area = p * m / 2;printf("%d\n",area + 1 -(sum/2));}
}


  相关解决方案