public class TriangleXor {
public int theArea(int W) {
double area = 0;
if (W % 2 == 0) area += W / 4.0;
double ratio = 0, prevX = 0;
for (int i = 1; i <= W; i++) {
double x = 1.0 * i * W / (W + i);
if (i % 2 == 1) {
ratio += x - prevX;
}
prevX = x;
}
area += (W / 4.0) * ratio / (W / 2.0) * 2;
int num = 1;
double bottomarea = 0;
for (int l = 1, r = W; l <= r; l++, r--) {
double x = (W - l) * 1.0 * (r - l) / (W + r - l) + l;
double y = 1.0 * (r - l) / W;
double prevY = 1.0 * (r - l + 1) / (W + r - l + 1);
double nextY = 1.0 * (r - l - 1) / (W + r - l - 1);
double dy = prevY - y;
if (r - l - 1 >= 0) dy += y - nextY;
double h = x - W / 2.0;
bottomarea += dy * h * num;
num += 2;
}
if (W % 2 == 0) {
area += bottomarea;
} else {
area += W / 4.0 - bottomarea;
}
return (int)area;
}
}