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 <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <unordered_map>
#include <iterator>
#include <functional>
#include <cassert>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define pb push_back
#define eb emplace_back
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const map<S, T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}
template<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}
template<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}

const int INF = 1<<28;
const double EPS = 1e-8;
const int MOD = 104857601;

template<int B, int MOD=104857601, int ROOT=3> // n <= 1<<B
struct FMT{
	int R[2*(B+1)];
	vi m2[(B+1)];
	vi power[2*(B+1)];
	FMT(){
		REP(e, (B+1)){
			m2[e].resize(1<<e);
			REP(m, 1<<e)REP(i,e) if(m&(1<<i)) m2[e][m] |= (1<<(e-1-i));
			
			int w1 = mod_pow(ROOT,(MOD-1)/(2<<e),MOD);
			int w2 = mod_inverse(w1 , MOD);
			power[  e].resize(1<<e);
			power[(B+1)+e].resize(1<<e);
			power[e][0] = power[(B+1)+e][0] = 1;
			REPS(j, (1<<e)-1){
				power[  e][j] = (ll)power[  e][j-1]*w1 % MOD;
				power[(B+1)+e][j] = (ll)power[(B+1)+e][j-1]*w2 % MOD;
			}
		}
	}

	void transform(int *f, int N, bool inv){
		int e;
		for(e=0;;e++) if(N == (1<<e)) break;
		REP(m,N)if(m < m2[e][m]) swap(f[m], f[m2[e][m]]);
		for(int t=0,t2=1;t2<N;t++,t2+=t2){
			int *po = power[inv*(B+1) + t].data();
			for(int i=0;i<N;i+=t2+t2){
				for (int j = 0; j < (1<<t); j++) {
					int x = f[i+j];
					int y = (ll)f[i+t2+j] * po[j] % MOD;
					f[i+j] = x + y;
					if(f[i+j] >= MOD) f[i+j] -= MOD;
					f[i+t2+j] = x - y;
					if(f[i+t2+j] < 0) f[i+t2+j] += MOD;
				}
			}
		}
		if(inv){
			int ni=mod_inverse(N,MOD);
			for(int i = 0; i < N; i++) f[i] = (ll)f[i] * ni % MOD;
		}
	}
	template <typename T>
	vi convolution(const vector<T> &f, const vector<T> &g){
		const int n = f.size(), m = g.size();
		int n2 = 1;
		while(n2 < 2*max(f.size(), g.size())) n2 *= 2;
		vi cf(n2), cg(n2);
		REP(i, n) cf[i] = f[i];
		REP(i, m) cg[i] = g[i];
		transform(cf.data(), n2, false);
		transform(cg.data(), n2, false);
		REP(i, n2) cf[i] = (ll)cf[i] * cg[i] % MOD;
		transform(cf.data(), n2, true);
		return cf;
	}
	ll mod_pow(ll a,ll b,ll M){
		if(b==0)return 1;
		ll res=mod_pow((a*a)%M,b/2,M);
		if(b&1)res=(res*a)%M;
		return res;
	}
	inline ll mod_inverse(ll a, ll m){
	  ll b = m, u = 1, v = 0;
	  while (b) {
	    ll t = a / b;
	    swap(a -= t * b, b);
	    swap(u -= t * v, v);
	  }
	  return (u % m + m) % m;
	}
};
FMT<12> fft;

int T, n, m;

int main(int argc, char *argv[]){
	scanf("%d", &n);
	int n2 = 1;
	while(n2 < 2*n) n2*=2;
	vector<vector<int>> f(n2, vector<int>(n2));
	auto g = f;
	int sum = 0;
	REP(i, n)REP(j, n){
		int x; scanf("%d", &x);
		g[n-1-i][n-1-j] = f[i][j] = x;
		sum += x;
	}
	REP(_, 2){
		REP(i, n2){
			fft.transform(f[i].data(), n2, false);
			fft.transform(g[i].data(), n2, false);
		}
		REP(i, n2) REP(j, i){
			swap(f[i][j], f[j][i]);
			swap(g[i][j], g[j][i]);
		}
	}
	REP(i, n2)REP(j, n2) f[i][j] = (ll)f[i][j]*g[i][j]%MOD;
	REP(_, 2){
		REP(i, n2)
			fft.transform(f[i].data(), n2, true);
		REP(i, n2) REP(j, i)
			swap(f[i][j], f[j][i]);
	}
	map<int, ll> cnt;
	double dsum = 0;
	ll all = 0;
	REP(i, 2*n)REP(j, 2*n){
		int x = f[i][j];
		int dy = abs(n - 1 - i);
		int dx = abs(n - 1 - j);
		if(x){
			if(dx+dy == 0 && !(x -= sum)) continue;
			cnt[dx*dx+dy*dy] += x;
			all += x;
			dsum += sqrt(dx*dx+dy*dy) * x;
		}
	}
	printf("%.20f\n", dsum / all);
	int putcnt = 0;
	for(auto it : cnt){
		printf("%d %lld\n", it.first, it.second/2);
		if(++ putcnt == 10000) break;
	}
	return 0;
}