Fence
时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte
总提交:59 测试通过:23
题目描述
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?
输入
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.
输出
A single line with a single integer for each test case that represents the number of cows the specified fence can hold.
样例输入
2
7 5 10
1 1 2
样例输出
20
0
题目链接:http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1434
题目大意:给出三个点,(n,m)为平面坐标系一点,p为x轴上一点,这两点和原点组成一个三角形,求这个三角形内的整数坐标点个数
题目分析:采用皮克定理S=a+b÷2-1,其中a表示多边形内部的点数,b表示多边形边界上的点数,s表示多边形的面积。
a即我们要求的,做下变换得到a = S + 1 - b/2,S = p * m / 2,又因为一条线段((m, n)、(p, q))上整点的个数为:gcd(m-p, n-q) + 1得b = gcd(m,n) + 1 + gcd(|n-p|, m) + 1 + p + 1 - 3(同一个点算了两次,所以要减去3个点多算得一次),得到:
a = p * m / 2 + 1 - (gcd(m,n) + gcd(|n-p|, m) + p) / 2
#include <cstdio> int abs(int x) { return x > 0 ? x : -x; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { int T, n, m, p; scanf("%d", &T); while(T--) { scanf("%d %d %d", &n, &m, &p); printf("%d\n", p * m / 2 + 1 - (gcd(m, n) + gcd(abs(n - p), m) + p) / 2); } }