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

Solution in C++14 (judge verdict: Accepted)

#include <iostream>
#include <ccomplex>
#include <vector>
#include <cassert>

using namespace std;

const double PI = acos(-1);

typedef complex<double> Comp;

bool is_pow_of2(int n) {
	int a = 1;
	while (n >= a) {
		if (n == a) {
			return true;
		}
		a <<= 1;
	}
	return false;
}


vector<Comp> fft(vector<Comp> v) {
	int n = v.size();
	assert(is_pow_of2(n));

	for (int j = 1, i = 0; j < n - 1; j++) {
		for (int k = n >> 1; k >(i ^= k); k >>= 1);
		if (j < i) swap(v[i], v[j]);
	}

	for (int m = 2; m <= n; m <<= 1) {
		double deg = (-1) * 2 * PI / m;
		Comp r(cos(deg), sin(deg));
		for (int i = 0;i < n;i += m) {
			Comp w(1, 0);
			for (int j = i, k = i + m / 2;k < i + m;j++, k++) {
				Comp t1 = v[j], t2 = w*v[k];
				v[j] = t1 + t2, v[k] = t1 - t2;
				w *= r;
			}
		}
	}
	return v;
}

vector<Comp> ifft(vector<Comp> v) {
	int n = v.size();
	assert(is_pow_of2(n));
	for (int j = 1, i = 0; j < n - 1; j++) {
		for (int k = n >> 1; k >(i ^= k); k >>= 1);
		if (j < i) swap(v[i], v[j]);
	}
	for (int m = 2; m <= n; m <<= 1) {
		double deg = 2 * PI / m;
		Comp r(cos(deg), sin(deg));
		for (int i = 0;i < n;i += m) {
			Comp w(1, 0);
			for (int j = i, k = i + m / 2;k < i + m;j++, k++) {
				Comp t1 = v[j], t2 = w*v[k];
				v[j] = t1 + t2, v[k] = t1 - t2;
				w *= r;
			}
		}
	}
	for (int i = 0; i < n;++i) v[i] *= 1.0 / n;
	return v;
}

vector<Comp> convolution(vector<Comp> P, vector<Comp> Q) {
	P = fft(P), Q = fft(Q);
	for (int i = 0;i < P.size();i++) P[i] *= Q[i];
	return ifft(P);
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0,i##_len=n;i<i##_len;i++)
#define all(x) begin(x),end(x)
#define lim(x,r,l) (r<=x&&x<l)

#define int long long

signed main(){
  int n; cin >> n;
  vector<vector<int>> c(n, vector<int>(n, 0));
  int zero = 0;
  rep(i,n)rep(j,n){
    int cc; cin >> cc;
    c[i][j] = cc;
    if(cc>=2) zero += (cc-1)*(cc) / 2;
  }
  vector<Comp> a(1 << 22), b(1 << 22);

  rep(i,n){rep(j,n){
    a[2*n * i + j] = c[i][j];
    b[2*n * i + j] = c[n - i - 1][n - j - 1];
  }}

  vector<Comp> res = convolution(a, b);

  double sum = 0;
  int num = 0;
  map<int, int> ans;

  rep(i, res.size()){
    int px = n - i / (2*n) - 1;
    int py = n - i % (2*n) - 1;
    int s = real(res[i]) + 0.5;
    int len = px*px + py*py;
    //cout << s << endl;
    if(s==0) {continue;}
    if(len == 0) {continue;}
    if(ans.find(len) != ans.end()){
      ans[len] += s;
    }else{
      ans[len] = s;
    }
    sum += sqrt(len) * s;
    num += s;
  }

  cout << setprecision(12) << sum / 2 /  (num / 2+ zero) << endl;
  int ii = 0;
  if(zero>0){
    cout <<"0 " << zero << endl;
    ii++;
  }
  for(auto aa: ans){
    if(ii>=10000) break;
    int l, s; tie(l, s) = aa;
    cout << l << " " << s / 2<< endl;
    ii++;
  }

}