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<stdio.h>
#include<algorithm>
#include<complex>
#include<vector>
#include<math.h>
#include<map>
using namespace std;
const double PI = 4.0*atan(1.0);
typedef complex<double> Complex;
const Complex I(0, 1);
void fft(int n, double theta, Complex a[]) {
  for (int m = n; m >= 2; m >>= 1) {
    int mh = m >> 1;
    for (int i = 0; i < mh; i++) {
      Complex w = exp(i*theta*I);
      for (int j = i; j < n; j += m) {
        int k = j + mh;
        Complex x = a[j] - a[k];
        a[j] += a[k];
        a[k] = w * x;
      }
    }
    theta *= 2;
  }
  int i = 0;
  for (int j = 1; j < n - 1; j++) {
    for (int k = n >> 1; k > (i ^= k); k >>= 1);
    if (j < i) swap(a[i], a[j]);
  }
}
int b[1025][1025];
Complex p[1<<22];
Complex q[1<<22];
int main(){
	int a;
	scanf("%d",&a);
	int cnt=0;
	for(int i=0;i<a;i++){
		for(int j=0;j<a;j++)scanf("%d",&b[i][j]);
	}
	int n=1;
	while(n<a*2)n*=2;
	for(int i=0;i<a;i++)for(int j=0;j<a;j++){
		cnt+=b[i][j];
		p[i*n+j]=q[(n-1-i)*n+(n-1-j)]=Complex(b[i][j],0);
	}
	fft(n*n,2*PI/n/n,p);
	fft(n*n,2*PI/n/n,q);
	
	for(int i=0;i<n*n;i++)p[i]=p[i]*q[i];
	fft(n*n,-2*PI/n/n,p);
	for(int i=0;i<n*n;i++)p[i]/=n*n;
	/*for(int i=0;i<n;i++){
		for(int j=0;j<n;j++)printf("%.0f ",p[i*n+j].real());
		printf("\n");
	}*/
	double ret=0;
	long long sz=0;
	map<int,long long>m;
	for(int i=0;i<n*n;i++){
			int pi=i/n;
			int pj=i%n;
			if(pj>=n/2)continue;
			if(pj==0&&pi>=n/2)continue;
			long long v=(long long)(p[n*n-1-i].real()+0.5);
			if(i==0){v-=cnt;v/=2;}
			if(!v)continue;
			
			int I=min(pi,n-pi);
			int J=pj;
			int t=I*I+J*J;
			if(m.count(t)){
				m[t]+=v;
			}else{
				m[t]=v;
			}
			sz+=v;
			ret+=sqrt(t)*v;
	}
	printf("%.12f\n",ret/sz);
	int at=0;
	for(map<int,long long>::iterator it=m.begin();it!=m.end();it++){
		if(at==10000)break;
		at++;
		printf("%d %lld\n",(*it).first,(*it).second);
	}
}