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 <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;

typedef double D;
typedef complex<D> P;
D pi=acos(-1);
void FFT(vector<P>& a,int N,double t){
	for(int m=N;m>=2;m/=2){
		int mh=m/2;
		rep(i,mh){
			P w=polar(1.0,t*i);
			for(int j=i;j<N;j+=m){
				int k=j+mh;
				P x=a[j]-a[k];
				a[j]+=a[k];
				a[k]=w*x;
			}
		}
		t*=2;
	}
	int i=0;
	rep1(j,N-2){
		for(int k=N/2;k>(i^=k);k/=2);
		if(j<i) swap(a[i],a[j]);
	}
}
vector<P> conv(vector<P> a,vector<P> b){
	int n=a.size()+b.size();
	int N=1;
	while(N<n) N*=2;
	a.resize(N);
	b.resize(N);
	FFT(a,N,2*pi/N);
	FFT(b,N,2*pi/N);
	rep(i,N) a[i]*=b[i];
	FFT(a,N,-2*pi/N);
	rep(i,N) a[i]/=N;
	return a;
}
inline long long fix(P x){
	return round(x.real());
}
int main(){
	int N;
	cin>>N;
	vector<P> A(N*N*2),B;
	rep(i,N) rep(j,N){
		cin>>A[i*2*N+j];
	}
	B=A;
	reverse(all(B));
	auto C=conv(A,B);
	vector<long long> mp(1<<21);
	rep(i,N) for(int j=-N+1;j<=N-1;j++){
		if(i==0&&j<=0) continue;
		mp[i*i+j*j]+=fix(C[2*N*N-1-i*2*N+j]);
	}
	rep(i,N) rep(j,N){
		int x=fix(A[i*2*N+j]);
		mp[0]+=x*(x-1)/2;
	}
	long long sum=0;
	double ave=0;
	rep(i,N*N*2){
		if(mp[i]==0) continue;
		ave+=sqrt(i)*mp[i];
		sum+=mp[i];
	}
	printf("%.12f\n",ave/sum);
	int c=0;
	rep(i,N*N*2){
		if(mp[i]==0) continue;
		printf("%d %lld\n",i,mp[i]);
		c++;
		if(c==10000) break;
	}
}