Molecule Pair Distance Histogram
Time limit10sMemory limit512 MB
From molecule counts on an N by N grid, find the mean pairwise Euclidean distance and the count of pairs at each squared distance.
- Level
Hard8 of 10
- Topics
- Divide and conquer, Matrix, Math, Geometry
- Solved
- No attempts yet
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 grid. The value in cell means that molecules sit at position . Two molecules in the same cell are at distance 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 ().
Each of the next lines contains integers. The -th number on the -th line is , the number of molecules at position ().
The grid holds at least molecules in total.
N
C11 C12 ... C1N
C21 C22 ... C2N
...
CN1 CN2 ... CNN
Output
On the first line print , 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 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 and the number of molecule pairs at that distance, separated by one space. Print only the distances with , in increasing order of .
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