USACO Electric Fence

Pick's theorem.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
ID: wcr19961
PROG: fence9
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;

int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}

int main() {
freopen("fence9.in", "r", stdin);
freopen("fence9.out", "w", stdout);
int n, m, p;
scanf("%d%d%d", &n, &m, &p);
int S = m * p / 2, B = gcd(n, m) + p + gcd(abs(p - n), m);
printf("%d\n", S - B / 2 + 1);
return 0;
}