### Triangles containing the origin题号：184 难度： 75 中英对照

Consider the set Ir of points (x,y) with integer co-ordinates in the interior of the circle with radius r, centered at the origin, i.e. x2 + y2 < r2.

For a radius of 2, I2 contains the nine points (0,0), (1,0), (1,1), (0,1), (-1,1), (-1,0), (-1,-1), (0,-1) and (1,-1). There are eight triangles having all three vertices in I2 which contain the origin in the interior. Two of them are shown below, the others are obtained from these by rotation.

For a radius of 3, there are 360 triangles containing the origin in the interior and having all vertices in I3 and for I5 the number is 10600.

How many triangles are there containing the origin in the interior and having all three vertices in I105?

### Code


public final class p184 {
public static void main(String[] args) {
long start=System.nanoTime();
String result = run();
long end=System.nanoTime();
System.out.println(result);
System.out.println( (end-start)/1000000 + "ms" );
}

static int R = 105;
static point[] Lattice = new point[4 * R * R];
static long[] straight = new long[4 * R * R];
static int Ln = 0;

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

static point Origin = new point(0, 0);

static public String run() {
for (int x = 0; x < R; x++)
for (int y = 0; y < R; y++)
if (x * x + y * y < R * R && !(x == 0 && y == 0)) {
Lattice[Ln++] = new point(x, y);
if (x > 0)
Lattice[Ln++] = new point(-x, y);
if (y > 0)
Lattice[Ln++] = new point(x, -y);
if (x > 0 && y > 0)
Lattice[Ln++] = new point(-x, -y);
}
for (int i = 0; i < Ln; i++) {
point curr = Lattice[i];
if (curr.x == 0)
straight[i] = 2 * (R - 1) - 1;
else {
int count = 0;
int x = curr.x, y = curr.y, c = gcd(x, y);
x /= c;
y /= c;
for (int jx = x;; jx += x) {
int jy = jx / x * y;
if (jx * jx + jy * jy >= R * R)
break;
count++;
}
straight[i] = 2 * count - 1;
}
}
long ans = 0;
for (int i = 0; i < Ln; i++) {
long s = straight[i], a;
a = (Ln - s) / 2;
ans += 2 * a * a - a * (a - 1) - (s+1)*a/2-a;
}
return (ans/6)+"";
}

static class point {
int x, y;

public point() {
}

public point(int xx, int yy) {
x = xx;
y = yy;
}
}

}
1725323624056
11ms