Problem:


F - Point Distance

Problem Statement

You work for invention center as a part time programmer. This center researches movement of protein molecules. It needs how molecules make clusters, so it will calculate distance of all pair molecules and will make a histogram.
Molecules’ positions are given by a N x N grid map.
Value C_{xy} of cell (x, y) in a grid map means that C_{xy} molecules are in the position (x, y).

You are given a grid map, please calculate histogram of all pair molecules.

Input

Input is formatted as follows.

N
C_{11} C_{12} ... C_{1N}
C_{21} C_{22} ... C_{2N}
...
C_{N1} C_{N2} ... C_{NN}

First line contains the grid map size N (1 \leq N \leq 1024).
Each next N line contains N numbers.
Each numbers C_{xy} (0 \leq C_{xy} \leq 9) means the number of molecule in position (x,y).
There are at least 2 molecules.

Output

Output is formatted as follows.

D_{ave}
d_{1} c_{1}
d_{2} c_{2}
...
d_{m} c_{m}

Print D_{ave} which an average distance of all pair molecules on first line.
Next, print histogram of all pair distance.
Each line contains a distance d_i and the number of pair molecules c_i(0 \lt c_i).
The distance d_i should be calculated by squared Euclidean distance and sorted as increasing order.
If the number of different distance is more than 10,000, please show the first 10,000 lines.
The answer may be printed with an arbitrary number of decimal digits, but may not contain an absolute or relative error greater than or equal to 10^{-8}.

Sample Input 1

2
1 0
0 1

Output for the Sample Input 1

1.4142135624
2 1

Sample Input 2

3
1 1 1
1 1 1
1 1 1

Output for the Sample Input 2

1.6349751825
1 12
2 8
4 6
5 8
8 2

Sample Input 3

5
0 1 2 3 4
5 6 7 8 9
1 2 3 2 1
0 0 0 0 0
0 0 0 0 1

Output for the Sample Input 3

1.8589994382
0 125
1 379
2 232
4 186
5 200
8 27
9 111
10 98
13 21
16 50
17 37
18 6
20 7
25 6



Sample input:
2
1 0
0 1


Sample output:
1.4142135624
2 1

Submission status: Runtime Error

Solution code:
import numpy as np
from scipy.signal import correlate

n = int( input() ) # Get grid width
c = []
for i in range(n):
	c.append( input().split() ) # Get line info
c = np.array(c, dtype=np.double)
R = np.int32(np.round(correlate(c,c, method='fft'))) # Autocorrelation w/ FFT

center = np.array([1, 1]) * (n-1)

histogram = {}
for y in range(R.shape[0]):
	for x in range(R.shape[1]):
		d = np.array([y, x])-center
		d2 = d@d
		v = R[y, x]
		if  np.round(v) != 0:
			if((d2 in histogram.keys()) == False):
				histogram[d2] = v
			else:
				histogram[d2] += v
histogram[0] =  (c*(c-1)).sum() # 
histogram = sorted( histogram.items() )

sum = 0
mean = 0
for d2, v in histogram: # \sum_n d_n V_n / \sum_n V_n
	sum += v
	mean += v * d2**.5
print(mean / sum)

for d2, v in histogram:
	if np.round(v) != 0:
		print(d2, int(np.round(v/2))) # Divide out double counting