Molecule Pair Distance Histogram

No attempts yetTime limit10sMemory limit512 MB

Problem

A research center studies how protein molecules move. To learn how the molecules gather into clusters, the center needs the distance between every pair of molecules, collected into a histogram.

Molecule positions are given as an N×NN \times N grid. The value CxyC_{xy} in cell (x,y)(x, y) means that CxyC_{xy} molecules sit at position (x,y)(x, y). Two molecules in the same cell are at distance 00 from each other.

Given the grid, compute the average distance over all pairs of two different molecules, together with the histogram of their squared distances.

Input

The first line contains the grid size NN (1N10241 \le N \le 1024).

Each of the next NN lines contains NN integers. The yy-th number on the xx-th line is CxyC_{xy}, the number of molecules at position (x,y)(x, y) (0Cxy90 \le C_{xy} \le 9).

The grid holds at least 22 molecules in total.

N
C11 C12 ... C1N
C21 C22 ... C2N
...
CN1 CN2 ... CNN

Output

On the first line print DavgD_{avg}, the average distance over all pairs of two different molecules, rounded to exactly six digits after the decimal point. Always print all six digits, so an average of 00 prints as 0.000000. The average uses the plain Euclidean distance, not the squared distance.

Then print the histogram of squared Euclidean distances. Each line holds a squared distance did_i and the number of molecule pairs cic_i at that distance, separated by one space. Print only the distances with ci>0c_i > 0, in increasing order of did_i.

If more than 10,000 distinct squared distances occur, print only the first 10,000 lines. The average on the first line still covers every pair.

D_avg
d1 c1
d2 c2
...
dm cm