USACO 3.4 Electric Fence

Pick’s Theorem

#include <vector>
#include <iostream>
#include <fstream>

using namespace std;

int gcd(int a, int b) {
if (b == 0) return a;
a %= b;
return gcd(b, a);
}

int n, m, p;
int main() {
ifstream fin("fence9.in");
ofstream fout("fence9.out");
fin >> n >> m >> p;
int ret = p * m / 2.0 - (gcd(m, n) + gcd(m, abs(p - n)) + p) / 2.0 + 1;
cout << ret << endl;
fout << ret << endl;
}