题目
Electric Fence
Don Piele
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?
PROGRAM NAME: fence9
INPUT FORMAT
The single input line contains three space-separated integers that denote n, m, and p.
SAMPLE INPUT (file fence9.in)
7 5 10
OUTPUT FORMAT
A single line with a single integer that represents the number of cows the specified fence can hold.
SAMPLE OUTPUT (file fence9.out)
20
题意(来自NOCOW)
在本题中,格点是指横纵坐标皆为整数的点。
为了圈养他的牛,农夫约翰(Farmer John)建造了一个三角形的电网。他从原点(0,0)牵出一根通电的电线,连接格点(n,m)(0<=n<32000,0<m<32000),再连接格点(p,0)(p>0),最后回到原点。
牛可以在不碰到电网的情况下被放到电网内部的每一个格点上(十分瘦的牛)。如果一个格点碰到了电网,牛绝对不可以被放到该格点之上(或许Farmer John会有一些收获)。那么有多少头牛可以被放到农夫约翰的电网中去呢?
输入格式
输入文件只有一行,包含三个用空格隔开的整数:n,m和p。
输出格式
输出文件只有一行,包含一个整数,代表能被指定的电网包含的牛的数目。
输入输出样例
输入 #1复制
7 5 10
输出 #1复制
20
思路
皮克定理 Pick's theorem(来自百度百科):
给定顶点坐标均是整点(或正方形格点)的简单多边形,皮克定理说明了其面积S和内部格点数目n、多边形边界上的格点数目s的关系:
所以n=S-s/2+1
求边上的点s
s=黄点gcd(n,m)+绿点gcd(abs(p-n),m)+红点p+1-2+蓝点1
代码
/*
ID:哎
TASK:fence9
LANG:C++
*/
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long
int n,m,p,ans;
int gcd(int a,int b)
{if(!b)return a;return gcd(b,a%b);
}
int main()
{freopen("fence9.in","r",stdin);freopen("fence9.out","w",stdout);scanf("%d%d%d",&n,&m,&p);int sid=gcd(n,m)+gcd(abs(p-n),m)+p;int s=(p*m)/2;ans=s-sid/2+1;printf("%d\n",ans);
}