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






Example

Input

2
1 0
0 1


Output

1.4142135624
2 1

Solution (in CPP):

Solution:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <complex>
using namespace std;

const double PI = acos(-1.0);
typedef long long LL;
typedef complex<double> Complex;

void FFT(Complex* P, int n, int op) {
	for(int i = 1, j = 0; i < n - 1; i++) {
		for(int s = n; j ^= s >>= 1, ~j&s; );
		if(i < j) swap(P[i], P[j]);
	}
	int log = 0;
	while((1 << log) < n) log++;
	for(int s = 0; s < log; s++) {
		int m = 1 << s;
		int m2 = m << 1;
		Complex wm(cos(PI / m), sin(PI / m) * op);
		for(int i = 0; i < n; i += m2) {
			Complex w(1, 0);
			for(int j = 0; j < m; j++, w *= wm) {
				Complex u = P[i + j];
				Complex t = P[i + j + m] * w;
				P[i + j] = u + t;
				P[i + j + m] = u - t;
			}
		}
	}
	if(op == -1) for(int i = 0; i < n; i++) P[i].real(P[i].real() / n);
}

Complex P[2][1 << 22];

int n;

LL cnt[2100000];

double dist(double x, double y) {
	return sqrt(x * x + y * y);
}

int main()
{
	scanf("%d", &n);
	int sum = 0;
	int offset = (n - 1) * (n * 2 + 1);
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			int x; scanf("%d", &x);
			sum += x;
			cnt[0] += (x - 1) * x / 2;
			int id = i * 2 * n + j;
			P[0][id] = x;
			P[1][offset-id] = x;
		}
	}

	int s = 1;
	while(s < offset * 2 + 1) s <<= 1;
	FFT(P[0], s, 1); FFT(P[1], s, 1);
	for(int i = 0; i < s; i++) P[0][i] *= P[1][i];
	FFT(P[0], s, -1);
	
	double ans = 0;
	for(int i = 1; i <= offset; i++) {
		LL t = (LL)(P[0][offset + i].real() + 0.5);
		if(!t) continue;
		int dx = ((i / n) + 1) >> 1;
		int dy = abs(i - dx * n * 2);
		ans += dist(dx, dy) * t;
		cnt[dx*dx+dy*dy] += t;
	}

	ans /= (double)sum * (sum - 1) / 2;
	printf("%.10f\n", ans);

	int num = 0;

	int top = (n - 1) * (n - 1) * 2;
	for(int i = 0; i <= top && num < 10000; i++) if(cnt[i]) {
		printf("%d %lld\n", i, cnt[i]);
		num++;
	}

	return 0;
}