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 <iostream>
#include <ccomplex>
#include <vector>
#include <cassert>
#include <iomanip>

using ll = long long;

using namespace std;

const double PI = acos(-1);

typedef complex<double> Comp;

bool is_pow_of2(int n) {
	int a = 1;
	while (n >= a) {
		if (n == a) {
			return true;
		}
		a <<= 1;
	}
	return false;
}


vector<Comp> fft(vector<Comp> v) {
	int n = v.size();
	assert(is_pow_of2(n));

	for (int j = 1, i = 0; j < n - 1; j++) {
		for (int k = n >> 1; k >(i ^= k); k >>= 1);
		if (j < i) swap(v[i], v[j]);
	}

	for (int m = 2; m <= n; m <<= 1) {
		double deg = (-1) * 2 * PI / m;
		Comp r(cos(deg), sin(deg));
		for (int i = 0;i < n;i += m) {
			Comp w(1, 0);
			for (int j = i, k = i + m / 2;k < i + m;j++, k++) {
				Comp t1 = v[j], t2 = w*v[k];
				v[j] = t1 + t2, v[k] = t1 - t2;
				w *= r;
			}
		}
	}
	return v;
}

vector<Comp> ifft(vector<Comp> v) {
	int n = v.size();
	assert(is_pow_of2(n));
	for (int j = 1, i = 0; j < n - 1; j++) {
		for (int k = n >> 1; k >(i ^= k); k >>= 1);
		if (j < i) swap(v[i], v[j]);
	}
	for (int m = 2; m <= n; m <<= 1) {
		double deg = 2 * PI / m;
		Comp r(cos(deg), sin(deg));
		for (int i = 0;i < n;i += m) {
			Comp w(1, 0);
			for (int j = i, k = i + m / 2;k < i + m;j++, k++) {
				Comp t1 = v[j], t2 = w*v[k];
				v[j] = t1 + t2, v[k] = t1 - t2;
				w *= r;
			}
		}
	}
	for (int i = 0; i < n;++i) v[i] *= 1.0 / n;
	return v;
}

vector<Comp> convolution(vector<Comp> P, vector<Comp> Q) {
	P = fft(P), Q = fft(Q);
	for (int i = 0;i < P.size();i++) P[i] *= Q[i];
	return ifft(P);
}


int main() {
    int N;
    cin >> N;
    int len = 1;
    while(N > len) {
        len *= 2;
    }

    int size = len*len*4;
    vector<Comp> a(size), b(size);
    ll total = 0;
    ll zero_cnt = 0;
    for(int i=0; i<N; ++i) {
        for(int j=0; j<N; ++j) {
            int c;
            cin >> c;
            zero_cnt += c*(c-1)/2;
            a[i*(2*len) + j] = c;
            b[(N-i)*(2*len) + N-j] = c;
        }
    }
    total += zero_cnt;
    auto t = convolution(a, b);
    vector<ll> v(N*N*2);
    for(int i=-N+1; i<=N-1; ++i) {
        for(int j=-N+1; j<=N-1; ++j) {
            v[i*i + j*j] += t[(N-i)*(2*len) + N + j].real() + 0.1;
        }
    }
    
    double d = 0;
    for(int i=0; i<v.size(); ++i) {
        if(v[i] > 0) {
            d += sqrt(i) * v[i]/2;
            if(i != 0) {
                total += v[i] / 2;
            }
        }
    }
    cout << fixed << setprecision(10) << d / total << endl;
    int cnt = 0;
    if(zero_cnt > 0) {
        cout << "0 " << zero_cnt << endl;
        cnt++;
    }
    for(int i=1; i<v.size(); ++i) {
        if(cnt == 10000) {
            break;
        }
        if(v[i] > 0) {
            cout << i << " " << v[i] / 2 << endl;
            cnt++;
        }
    }
}