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 <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
typedef double db;
typedef long long ll;
const db pi = acos(-1.0);
const int N = 1 << 21 | 5; // 2n^2
const int M = 1 << 22 | 5;
struct comp_t {
	db x, y;
	comp_t(db a = 0, db b = 0) : x(a), y(b) {}
	comp_t operator + (const comp_t &b) const { return (comp_t) {x + b.x, y + b.y}; }
	comp_t operator - (const comp_t &b) const { return (comp_t) {x - b.x, y - b.y}; }
	comp_t operator * (const comp_t &b) const { return (comp_t) {x * b.x - y * b.y, b.x * y + x * b.y}; }
	inline void rev() { y = -y; }
} wnk[M];
int lim, r[M], A[M], B[M], n;
ll ans[N];
void init(int len) {
	int l = 0;
	for(lim = 1; lim <= len; lim <<= 1) l ++;
	for(int i = 1; i < lim; i ++) {
		r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));
	}
	wnk[0] = comp_t(1, 0);
	wnk[1] = comp_t(cos(2 * pi / lim), sin(2 * pi / lim));
	for(int i = 2; i < lim; i ++) wnk[i] = wnk[i - 1] * wnk[1];
}
void fft(comp_t *a, int t) {
	for(int i = 1; i < lim; i ++)
		if(i < r[i]) swap(a[i], a[r[i]]);
	for(int i = 1; i < lim; i <<= 1) {
		for(int j = 0; j < lim; j += i << 1) {
			for(int k = 0; k < i; k ++) {
				comp_t y = wnk[lim / (i << 1) * k];
				if(t == -1) y.rev();
				comp_t p = a[j + k], q = y * a[j + k + i];
				a[j + k] = p + q; a[j + k + i] = p - q;
			}
		}
	}
}
ll *const_mul(int *a, int *b, int n, int m) {
	static ll ans[M];
	static comp_t A[M], B[M];
	init(n + m);
	for(int i = 0; i < lim; i ++) A[i] = comp_t(i <= n ? a[i] : 0);
	for(int i = 0; i < lim; i ++) B[i] = comp_t(i <= m ? b[i] : 0);
	fft(A, 1); fft(B, 1);
	for(int i = 0; i < lim; i ++) A[i] = A[i] * B[i];
	fft(A, -1);
	for(int i = 0; i <= n + m; i ++) ans[i] = (ll) (A[i].x / lim + 0.5);
	return ans;
}
int main() {
	scanf("%d", &n);
	int ma = n * (2 * n - 1) - 1, s = 0;
	for(int x, i = 0; i < n; i ++) {
		for(int j = 0; j < n; j ++) {
			scanf("%d", &x); s += x;
			A[2 * n * i + j] += x;
		}
	}
	copy(A, A + ma + 1, B); reverse(B, B + ma + 1);
	ll *t = const_mul(A, B, ma, ma);
	for(int i = 0; i <= (ma << 1); i ++) if(t[i]) {
		ll val = i - ma, dy = (val % (n * 2) + (n * 2)) % (n * 2);
		if(dy > n) dy -= n * 2;
		ll dx = (val - dy) / (n * 2);
		ans[dx * dx + dy * dy] += t[i];
	}
	ll tot = 0; db sum = 0;
	for(int i = 0; i < 2 * n * n; i ++) if(ans[i]) {
		if(!i) ans[i] -= s;
		ans[i] /= 2;
		sum += sqrt(i) * ans[i]; tot += ans[i];
	}
	printf("%.10f\n", (double) (sum / tot));
	int pr_lim = 10000;
	for(int i = 0; i < 2 * n * n; i ++) if(ans[i]) {
		printf("%d %lld\n", i, ans[i]);
		if(-- pr_lim == 0) break ;
	}
	return 0;
}