Problem p01672 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
using namespace std;

const double Pi = acos(-1.0);
typedef long long ll;

void Read(int &p) {
  p = 0;
  char c = getchar();
  for (; c < '0' || c > '9'; c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar())
    p = p * 10 + c - '0';
}
struct cpx {
  double r, i;
  cpx() {
    r = 0;
    i = 0;
  }
  cpx(double a, double b) { r = a, i = b; }
  cpx operator+(cpx b) { return cpx(r + b.r, i + b.i); }
  cpx operator-(cpx b) { return cpx(r - b.r, i - b.i); }
  cpx operator*(cpx b) { return cpx(r * b.r - i * b.i, r * b.i + i * b.r); }
  cpx operator*=(cpx b) { return *this = *this * b; }
  cpx operator/=(double a) { return *this = cpx(r / a, i / a); }
} a[1 << 22], b[1 << 22];

void DFT(cpx *A, int f, int w) {
  for (int i = 1, j = 0; i < w; i++) {
    for (int d = w >> 1; j ^= d, ~j & d; d >>= 1)
      ;
    if (i < j)
      swap(A[i], A[j]);
  }
  for (int i = 1; i < w; i <<= 1) {
    cpx wn(cos(Pi / i), f * sin(Pi / i));
    for (int j = 0; j < w; j += i << 1) {
      int k = j + i;
      cpx w1(1, 0);
      for (int l = 0; l < i; l++) {
        cpx x = A[j + l], y = w1 * A[k + l];
        A[j + l] = x + y;
        A[k + l] = x - y;
        w1 *= wn;
      }
    }
  }
  if (f == -1)
    for (int i = 0; i < w; i++)
      A[i] /= w;
}

int n, sum;
int C[1 << 10][1 << 10];
ll ans[2100000];

int main() {
  scanf("%d", &n);
  int w = 1, setlen = (n - 1) * (n * 2 + 1);
  while (w <= setlen * 2)
    w <<= 1;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      Read(C[i][j]);
      sum += C[i][j];
      ans[0] += C[i][j] * (C[i][j] - 1) / 2;
      int id = i * 2 * n + j;
      a[id].r = C[i][j];
      b[setlen - id].r = C[i][j];
    }
  DFT(a, 1, w), DFT(b, 1, w);
  for (int i = 0; i < w; i++)
    a[i] = a[i] * b[i];
  DFT(a, -1, w);
  double ans1 = 0;
  for (int i = 1; i <= setlen; i++) {
    ll t = (ll)(a[setlen + i].r + 0.3);
    if (!t)
      continue;
    int dx = ((i / n) + 1) >> 1;
    int dy = abs(i - dx * n * 2);
    ans1 += sqrt((double)dx * dx + (double)dy * dy) * t;
    ans[dx * dx + dy * dy] += t;
  }

  ans1 /= (double)sum * (sum - 1) / 2;
  printf("%.10f\\n", ans1);

  int num = 0;

  int top = (n - 1) * (n - 1) * 2;
  for (int i = 0; i <= top && num < 10000; i++)
    if (ans[i]) {
      printf("%d %lld\\n", i, ans[i]);
      num++;
    }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define M 2100000
#define N 1050
#define ll long long
double Pi = acos(-1);
void Read(int &p) {
  p = 0;
  char c = getchar();
  for (; c < '0' || c > '9'; c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar())
    p = p * 10 + c - '0';
}
struct cpx {
  double r, i;
  cpx() {
    r = 0;
    i = 0;
  }
  cpx(double a, double b) { r = a, i = b; }
  cpx operator+(cpx b) { return cpx(r + b.r, i + b.i); }
  cpx operator-(cpx b) { return cpx(r - b.r, i - b.i); }
  cpx operator*(cpx b) { return cpx(r * b.r - i * b.i, r * b.i + i * b.r); }
  cpx operator*=(cpx b) { return *this = *this * b; }
  cpx operator/=(double a) { return *this = cpx(r / a, i / a); }
} a[M], b[M];
int n, sum, C[N][N];
ll ans0;
void DFT(cpx *A, int f, int w) {
  for (int i = 1, j = 0; i < w; i++) {
    for (int d = w >> 1; j ^= d, ~j & d; d >>= 1)
      ;
    if (i < j)
      swap(A[i], A[j]);
  }
  for (int i = 1; i < w; i <<= 1) {
    cpx wn(cos(Pi / i), f * sin(Pi / i));
    for (int j = 0; j < w; j += i << 1) {
      int k = j + i;
      cpx w1(1, 0);
      for (int l = 0; l < i; l++) {
        cpx x = A[j + l], y = w1 * A[k + l];
        A[j + l] = x + y;
        A[k + l] = x - y;
        w1 *= wn;
      }
    }
  }
  if (f == -1)
    for (int i = 0; i < w; i++)
      A[i] /= w;
}
ll ans[M];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      Read(C[i][j]);
      sum += C[i][j];
      ans0 += C[i][j] * (C[i][j] - 1) / 2;
    }
  int w = 1, setlen = (n - 1) * (n * 2 + 1);
  while (w <= setlen * 2)
    w <<= 1;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      int id = i * 2 * n + j;
      a[id].r = C[i][j];
      b[setlen - id].r = C[i][j];
    }
  DFT(a, 1, w), DFT(b, 1, w);
  for (int i = 0; i < w; i++)
    a[i] = a[i] * b[i];
  DFT(a, -1, w);
  double ans1 = 0;
  for (int i = 1; i <= setlen; i++) {
    ll t = (ll)(a[setlen + i].r + 0.3);
    if (!t)
      continue;
    int dx = ((i / n) + 1) / 2;
    int dy = abs(i - dx * n * 2);
    ans1 += sqrt((double)dx * dx + (double)dy * dy) * t;
    ans[dx * dx + dy * dy] += t;
  }
  ans1 /= (double)sum * (sum - 1) / 2;
  printf("%.10lf\\n", ans1);
  if (ans0)
    printf("0 %lld\\n", ans0);
  for (int i = 1; i <= 2 * n * n; i++)
    if (ans[i])
      printf("%d %lld\\n", i, ans[i]);
}