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 <cassert>
#include <complex>

#define rep(i,n) for(int i=0;i<(n);i++)
#define complex  complex<double>

using namespace std;

const double EPS=1e-5;
const double PI=acos(-1);
const complex I(0,1); // 虚数単位

const int N_MAX=1024;
const int D_MAX=2*N_MAX*N_MAX;

complex operator*(complex z,double r){ return complex(z.real()*r,z.imag()*r); }
complex operator/(complex z,double r){ return complex(z.real()/r,z.imag()/r); }

complex e[N_MAX];

void FFT_rec(int n,const complex *f,int s,complex *res){
	if(n==1){
		res[0]=f[0];
		return;
	}

	FFT_rec(n/2, f ,2*s,  res  ); // even part
	FFT_rec(n/2,f+s,2*s,res+n/2); // odd part

	rep(k,n/2){
		complex x=res[  k  ];
		complex y=res[k+n/2]*e[s*k]; // e[s*k] = exp(-2*PI*I*k/n)
		res[  k  ]=x+y;
		res[k+n/2]=x-y;
	}
}

complex tmp[2*N_MAX];
void FFT(int n,complex *f){
	FFT_rec(n,f,1,tmp);
	rep(i,n) f[i]=tmp[i];
}

void IFFT(int n,complex *f){
	rep(i,n) if(i%2!=0) f[i]*=-1; // f[n/2] を中心にするための trick
	FFT_rec(n,f,1,tmp);
	rep(i,n) f[i]=tmp[i]/n;
}

void FFT_2D(int n,complex f[2*N_MAX][2*N_MAX]){
	rep(i,n) FFT(n,f[i]);
	rep(i,n) rep(j,i) swap(f[i][j],f[j][i]); // transpose
	rep(i,n) FFT(n,f[i]);
	rep(i,n) rep(j,i) swap(f[i][j],f[j][i]);
}

void IFFT_2D(int n,complex f[2*N_MAX][2*N_MAX]){
	rep(i,n) IFFT(n,f[i]);
	rep(i,n) rep(j,i) swap(f[i][j],f[j][i]);
	rep(i,n) IFFT(n,f[i]);
	rep(i,n) rep(j,i) swap(f[i][j],f[j][i]);
}

int main(){
	int n; scanf("%d",&n);
	static int dat[N_MAX][N_MAX];
	rep(i,n) rep(j,n) scanf("%d",dat[i]+j);

	for(;(n&(n-1))!=0;n++); // n を 2 のべきまで増やす

	rep(k,n) e[k]=exp(-2*PI*I*k/(2*n));

	static complex f[2*N_MAX][2*N_MAX];
	static complex g[2*N_MAX][2*N_MAX];
	const int OFS=n; // offset
	rep(i,n) rep(j,n) {
		f[ i+OFS][ j+OFS]=dat[i][j];
		g[-i+OFS][-j+OFS]=dat[i][j];
	}

	FFT_2D(2*n,f);
	FFT_2D(2*n,g);

	rep(i,2*n) rep(j,2*n) f[i][j]*=g[i][j];

	IFFT_2D(2*n,f);

	rep(i,2*n) rep(j,2*n) {
		assert(abs(f[i][j].real()-round(f[i][j].real()))<EPS);
		assert(abs(f[i][j].imag())<EPS);
	}

	static long long ans[D_MAX];
	rep(i,2*n) rep(j,2*n) ans[(i-OFS)*(i-OFS)+(j-OFS)*(j-OFS)]+=(int)(f[i][j].real()+0.5);

	// 非順序対を数えたいので /2
	rep(d,D_MAX) if(d>0) {
		assert(ans[d]%2==0);
		ans[d]/=2;
	}

	// 距離 0 だけ例外
	ans[0]=0;
	rep(i,n) rep(j,n) ans[0]+=dat[i][j]*(dat[i][j]-1)/2;

        long double aved = 0.0;
        long double cnt = 0.0;
        rep(d,D_MAX) {
          aved += sqrt(d) * ans[d];
          cnt += ans[d];
        }
        aved /= cnt;
        printf("%.10Lf\n", aved);

        int index = 0;
        rep(d,D_MAX) {
          if(ans[d]>EPS) {
            index++;
            printf("%d %lld\n",d,ans[d]);
            if (index == 10000) { break; }
          }
        }

	return 0;
}