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"

using namespace std;

using ld = long double;
using ll = long long;
class NTT
{
    using ll = long long;
    // mod = 998244353
    const int mod = 7 * 17 * (1 << 23) + 1;

    static const int omega_max = 23;
    static const int root = 31;
    std::vector<int> omega;

    int mod_inv(ll a, ll m)
    {
        ll b = mod, u = 1, v = 0;
        while (b > 0)
        {
            ll t = a / b;
            a -= t * b;
            std::swap(a, b);
            u -= t * v;
            std::swap(u, v);
        }
        return (u % m + m) % m;
    }
    std::vector<int> fft(std::vector<int> v, bool inv)
    {
        const int n = v.size();
        // assert(n == 2冪);
        int _logn = 0;
        while ((1 << _logn) < n)
            _logn++;
        assert(1 << _logn == n);
        int ww = omega[_logn];
        if (inv)
            ww = mod_inv(ww, mod);
        for (int m = n; m >= 2; m >>= 1)
        {
            const int mh = m >> 1;
            int w = 1;
            for (int i = 0; i < mh; i++)
            {
                for (int j = i; j < n; j += m)
                {
                    const int k = j + mh;
                    int x = v[j] - v[k];
                    if (x < 0)
                        x += mod;
                    v[j] += v[k];
                    if (v[j] >= mod)
                        v[j] -= mod;
                    v[k] = (1LL * w * x) % mod;
                }
                w = (1LL * w * ww) % mod;
            }
            ww = (1LL * ww * ww) % mod;
        }

        int i = 0;
        for (int j = 1; j < n - 1; j++)
        {
            for (int k = n >> 1; k > (i ^= k); k >>= 1)
                ;
            if (j < i)
                std::swap(v[i], v[j]);
        }
        if (inv)
        {
            const int inv_n = mod_inv(n, mod);
            for (auto &x : v)
            {
                x = (1LL * x * inv_n) % mod;
            }
        }
        return v;
    }

  public:
    NTT()
    {
        omega.resize(omega_max + 1);
        int x = root;
        for (int i = omega_max; i >= 0; i--)
        {
            omega[i] = x;
            x = (1LL * x * x) % mod;
        }
    }
    std::vector<int> convolution(std::vector<int> f, std::vector<int> g)
    {
        int sz = 1;
        const int m = f.size() + g.size() - 1;
        while (sz < m)
            sz *= 2;
        f.resize(sz), g.resize(sz);
        f = this->fft(std::move(f), false);
        g = this->fft(std::move(g), false);
        for (int i = 0; i < sz; i++)
        {
            f[i] = (1LL * f[i] * g[i]) % mod;
        }
        return this->fft(std::move(f), true);
    }
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    vector<int> x(n * n * 2);
    int zero = 0;
    ld sum = 0, cnt = 0;
    NTT ntt;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            int c;
            cin >> c;
            x[2 * i * n + j] = c;
            zero += c * (c - 1) / 2;
        }
    }
    map<int, ll> count;
    if (zero > 0)
    {
        cnt = zero;
        count[0] = zero;
    }
    vector<int> y = x;
    reverse(y.begin(), y.end());
    auto ret = ntt.convolution(x, y);
    for (int i = 0; i < n; i++)
    {
        for (int j = -n + 1; j < n; j++)
        {
            if (i == 0 && j <= 0)
                continue;
            const int d = int(ret[2 * n * n - 1 + 2 * i * n + j]);
            if (d > 0)
                count[i * i + j * j] += d;
            cnt += d;
            sum += sqrt(ld(i * i + j * j)) * d;
        }
    }
    cout << fixed << setprecision(10) << sum / cnt << endl;
    int line = 0;
    for (auto p : count)
    {
        cout << p.first << " " << p.second << endl;
        line++;
        if (line >= 10000)
            break;
    }
    return 0;
}