Problem:
Problem H: Inherit the Spheres In the year 2xxx, an expedition team landing on a planet found strange objects made by an
ancient species living on that planet. They are transparent boxes containing opaque solid spheres
(Figure 1). There are also many lithographs which seem to contain positions and radiuses of
spheres.   Figure 1: A strange object  Initially their objective was unknown, but Professor Zambendorf found the cross section formed
by a horizontal plane plays an important role. For example, the cross section of an object
changes as in Figure 2 by sliding the plane from bottom to top.   Figure 2: Cross sections at different positions  He eventually found that some information is expressed by the transition of the number of
connected figures in the cross section, where each connected figure is a union of discs intersecting
or touching each other, and each disc is a cross section of the corresponding solid sphere. For
instance, in Figure 2, whose geometry is described in the first sample dataset later, the number
of connected figures changes as 0, 1, 2, 1, 2, 3, 2, 1, and 0, at z = 0.0000, 162.0000, 167.0000,
173.0004, 185.0000, 191.9996, 198.0000, 203.0000, and 205.0000, respectively. By assigning 1 for
increment and 0 for decrement, the transitions of this sequence can be expressed by an 8-bit
binary number 11011000. For helping further analysis, write a program to determine the transitions when sliding the
horizontal plane from bottom ( z = 0) to top ( z = 36000). Input The input consists of a series of datasets. Each dataset begins with a line containing a positive
integer, which indicates the number of spheres N in the dataset. It is followed by N lines
describing the centers and radiuses of the spheres. Each of the N lines has four positive integers X i , Y i , Z i , and R i ( i = 1, . . . , N ) describing the center and the radius of the i -th sphere,
respectively. You may assume 1 ≤ N ≤ 100, 1 ≤ R i ≤ 2000, 0 < X i - R i < X i + R i < 4000, 0 < Y i - R i < Y i + R i < 16000, and 0 < Z i - R i < Z i + R i < 36000. Each solid sphere is defined as the set of
all points ( x , y , z ) satisfying ( x - X i ) 2 + ( y - Y i ) 2 + ( z - Z i ) 2 ≤ R i 2 . A sphere may contain other spheres. No two spheres are mutually tangent. Every Z i ± R i and
minimum/maximum z coordinates of a circle formed by the intersection of any two spheres differ
from each other by at least 0.01. The end of the input is indicated by a line with one zero. Output For each dataset, your program should output two lines. The first line should contain an integer M indicating the number of transitions. The second line should contain an M -bit binary number
that expresses the transitions of the number of connected figures as specified above. Sample Input 3
95 20 180 18
125 20 185 18
40 27 195 10
1
5 5 5 4
2
5 5 5 4
5 5 5 3
2
5 5 5 4
5 7 5 3
16
2338 3465 29034 710
1571 14389 25019 842
1706 8015 11324 1155
1899 4359 33815 888
2160 10364 20511 1264
2048 8835 23706 1906
2598 13041 23679 618
1613 11112 8003 1125
1777 4754 25986 929
2707 9945 11458 617
1153 10358 4305 755
2462 8450 21838 934
1822 11539 10025 1639
1473 11939 12924 638
1388 8519 18653 834
2239 7384 32729 862
0 Output for the Sample Input 8
11011000
2
10
2
10
2
10
28
1011100100110101101000101100

Solution (Python):
from collections import defaultdict, deque
import sys
readline = sys.stdin.readline
write = sys.stdout.write
def solve():
    N = int(readline())
    if N == 0:
        return False
    C0 = [list(map(int, readline().split())) for i in range(N)]
    U = [1]*N
    D = defaultdict(list)
    for i in range(N):
        xi, yi, zi, ri = C0[i]
        for j in range(i):
            xj, yj, zj, rj = C0[j]
            dd = (xi - xj)**2 + (yi - yj)**2 + (zi - zj)**2
            if dd <= (ri - rj)**2:
                if ri < rj:
                    U[i] = 0
                else:
                    U[j] = 0
    C = [e for i, e in enumerate(C0) if U[i]]
    N = len(C)
    EPS = 1e-9
    for i in range(N):
        xi, yi, zi, ri = C[i]
        D[zi+ri].append((0, i, 0))
        D[zi-ri].append((0, i, 0))
        for j in range(i):
            xj, yj, zj, rj = C[j]
            dd = (xi - xj)**2 + (yi - yj)**2 + (zi - zj)**2
            if dd > (ri + rj)**2:
                continue
            def check(z):
                si = (ri**2 - (z - zi)**2)**.5
                sj = (rj**2 - (z - zj)**2)**.5
                return (xi - xj)**2 + (yi - yj)**2 <= (si + sj)**2
            z0 = zi + (zj - zi) * (ri**2 + dd - rj**2) / (2 * dd)
            zm = min(zi+ri, zj+rj)
            if check(zm):
                z1 = zm
            else:
                left = 0; right = zm - z0
                while right - left > EPS:
                    mid = (left + right) / 2
                    
                    if check(z0 + mid):
                        left = mid
                    else:
                        right = mid
                z1 = z0 + left
            zm = max(zi-ri, zj-rj)
            if check(zm):
                z2 = zm
            else:
                left = 0; right = z0 - max(zi-ri, zj-rj)
                while right - left > EPS:
                    mid = (left + right) / 2
                    
                    if check(z0 - mid):
                        left = mid
                    else:
                        right = mid
                z2 = z0 - left
            D[z1].append((1, i, j))
            D[z2].append((1, i, j))

    res = [0]
    E = [[0]*N for i in range(N)]
    U = [0]*N
    *zs, = D.items()
    zs.sort()
    for z, es in zs:
        for t, a, b in es:
            if t:
                E[a][b] = E[b][a] = E[a][b] ^ 1
            else:
                U[a] ^= 1
        c = 0
        que = deque()
        used = [0]*N
        for i in range(N):
            if used[i] or not U[i]:
                continue
            c += 1
            v = 0
            used[i] = 1
            que.append(i)
            while que:
                v = que.popleft()
                for w in range(N):
                    if not E[v][w] or used[w]:
                        continue
                    used[w] = 1
                    que.append(w)
        if res[-1] != c:
            res.append(c)
    ans = []
    for i in range(len(res)-1):
        if res[i] < res[i+1]:
            ans.append("1")
        else:
            ans.append("0")
    write("%d\n" % len(ans))
    write("".join(ans))
    write("\n")
    return True
while solve():
    ...