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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <climits>
#include <complex>
#include <numeric>
using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define RREP(i,n) for(int i=(int)n-1; 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(c) (c).begin(), (c).end()

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int> > pipii;
typedef vector<int> vi;

const int INF = 1e9;
const int MOD = 1e9+7;

typedef complex<double> Comp;
typedef vector<Comp> vc;
void fft(vc &f, bool inv = false){
    int n = f.size();
    for(int i = 0, j = 1; j < n - 1; j++){
        for(int k = n>>1; k > (i ^= k); k>>=1);
        if(i > j) swap(f[i], f[j]);
    }
    for(int m = 2; m <= n; m<<=1){
        double deg = (inv ? -1 : 1) * 2 * M_PI / m;
        Comp zeta = Comp(cos(deg), sin(deg));
        for(int i = 0; i < n; i += m){   
            Comp pow_zeta = Comp(1.0, 0.0);
            for(int j1 = i, j2 = i + m / 2; j2 < i + m; j1++, j2++){
                Comp t1 = f[j1], t2 = pow_zeta * f[j2];
                f[j1] = t1 + t2, f[j2] = t1 - t2;
                pow_zeta *= zeta;
            }
        }
    }
    return;
}
void multiply(vector<vc> &g, vector<vc> &h){
    int n = 1;
    while(n < g.size() + h.size()) n<<=1;
    g.resize(n, vc(n, Comp(0.0, 0.0)));
    h.resize(n, vc(n, Comp(0.0, 0.0)));
    REP(i, n){
        g[i].resize(n, Comp(0.0, 0.0));
        h[i].resize(n, Comp(0.0, 0.0));
    }
    REP(i, n){
        fft(g[i]); fft(h[i]);
    }
    vc x(n), y(n);
    REP(j, n){
        REP(i, n){
            x[i] = g[i][j];
            y[i] = h[i][j];
        }
        fft(x);
        fft(y);
        REP(i, n){
            g[i][j] = x[i];
            h[i][j] = y[i];
        }
    }
    REP(i, n){
        REP(j, n){
            g[i][j] *= h[i][j];
        }
    }
    //inverse
    REP(j, n){
        REP(i, n){
            x[i] = g[i][j];
        }
        fft(x, true);
        REP(i, n){
            g[i][j] = x[i];
        }
    }
    REP(i, n){
        fft(g[i], true);
    }
    REP(i, n){
        REP(j, n){
            g[i][j] /= n * n;
        }
    }
    return;
}
int n;

int main(void){
    cin >> n;
    vector<vc> g(n, vc(n)), g2(n, vc(n));
    ll num = 0;
    REP(i, n){
        REP(j, n){
            int x;
            scanf("%d", &x);
            g[i][j] = g2[n - i - 1][n - j - 1] = Comp((double)x, 0.0);
            num -= (ll)(real(g[i][j]) + 0.01);
        }
    }
    //cout << g.size() << ":";
    multiply(g, g2);
    //cout << g.size() << endl;
    //map<ll, ll> ans;
    vector<ll> ans(3 * n * n, 0);
    double dist = 0.0;
    ans[0] = num;
    REP(i, g.size()){
        REP(j, g[i].size()){
            ll dn;
            int x = abs(i - (n - 1)), y = abs(j - (n - 1));
            if((dn = real(g[i][j] + 0.01)) > 0){
                ll at = (ll)(x * x + y * y);
                num += dn;
                dist += dn * sqrt((double)at);
                ans[at] += (ll)(real(g[i][j]) + 0.01);
            }
        }
    }
    printf("%.10lf\n", dist / num);
    num = 0;
    REP(i, ans.size()){
        if(ans[i] / 2  == 0) continue;
        if(num++ == 10000) break;
        cout << i << " " << ans[i] / 2  << endl;
    }
    return 0;
}