USACO3.4 Electric Fence

先丢题面跑:

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).

为了管理他的牛们,FJ用几条通着电的铁丝建造了一个三角形的围栏,从原点(0,0)到整点(n,m),再到x正半轴上的点(p,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.

一行包含 n,m,p。

SAMPLE INPUT (file fence9.in) 样例输入(文件 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) 样例输出(文件 fence9.out)

20

题解

可以算是一道数学题吧。

如果知道皮克定理就好写多了。

皮克定理说明了其面积S和内部格点数目a、边上格点数目b的关系:S = a + b/2 - 1。

根据三角形面积公式求出S。如果知道了b,那么三角形内部格点数目a也就求出来了。

可以证明,一条直线((0,0),(n,m))上的格点数等于n与m的最大公约数+1。 即b=gcd(n,m)+1. gcd(n,m)为n与m的最大公约数。

代入皮克公式,即可求出a的值.

附上代码:

 1 /*
 2 ID: hanghan1
 3 LANG: C++11
 4 PROB: fence9
 5 */
 6 #define _TASK_ "fence9"
 7 #include <bits/stdc++.h>
 8 using namespace std;
 9 #define gcd __gcd // for gcd support
10 
11 int main() {
12     freopen(_TASK_ ".in", "r", stdin);
13     freopen(_TASK_ ".out", "w", stdout);
14     int n,m,p; scanf("%d%d%d", &n,&m,&p);
15     printf("%d\n", (p*m - gcd(n, m) - p - gcd(n+p, m) + 5 >> 1) - 1);
16     return 0;
17 }

猜你喜欢

转载自www.cnblogs.com/mchmch/p/usaco-3-1-fence9.html