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

#include <bits/stdc++.h>
using namespace std;
// #define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define dbv(v)                                                                 \\
  cerr << #v << "=";                                                           \\
  for (auto _x : v)                                                            \\
    cerr << _x << ", ";                                                        \\
  cerr << endl
#define dba(a, n)                                                              \\
  cerr << #a << "=";                                                           \\
  for (int _i = 0; _i < (n); ++_i)                                             \\
    cerr << a[_i] << ", ";                                                     \\
  cerr << endl
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &x) {
  return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
namespace fft {
struct cpx {
  double r, i;
  cpx(double r = 0, double i = 0) : r(r), i(i) {}
  cpx operator+(cpx b) { return {r + b.r, i + b.i}; }
  cpx operator-(cpx b) { return {r - b.r, i - b.i}; }
  cpx operator*(cpx b) { return {r * b.r - i * b.i, r * b.i + i * b.r}; }
  cpx operator/(double b) { return {r / b, i / b}; }
  inline cpx conj() { return {r, -i}; }
};
const double PI = acos(-1);
vector<int> rev(1, 0); // rev has [base] bits
vector<cpx> roots;
int base = 0;
void precompute(int nbase) {
  if (nbase <= base)
    return;
  rev.resize(1 << nbase);
  for (int i = 0; i < (1 << nbase); ++i) {
    rev[i] = rev[i >> 1] >> 1 | ((i & 1) << nbase - 1);
  }
  roots.resize(1 << nbase);
  for (; base < nbase; ++base) {
    int len = 1 << base;
    double angle = 2 * PI / (1 << (base + 1));
    for (int i = 0; i < len; ++i) {
      double cur = angle * i;
      roots[len + i] = cpx(cos(cur), sin(cur));
    }
  }
}
void fft(vector<cpx> &a, int n = -1) {
  if (n == -1)
    n = a.size();
  assert((n & (n - 1)) == 0);
  int zeros = __builtin_ctz(n);
  precompute(zeros);
  int shift = base - zeros;
  for (int i = 0; i < n; ++i) {
    if (i < (rev[i] >> shift))
      swap(a[i], a[rev[i] >> shift]);
  }
  for (int len = 1; len < n; len <<= 1)
    for (int i = 0; i < n; i += 2 * len)
      for (int j = 0; j < len; ++j) {
        cpx u = a[i + j], v = a[i + j + len] * roots[len + j];
        a[i + j] = u + v;
        a[i + j + len] = u - v;
      }
}
vector<cpx> fa, fb;
vector<ll> multiply(const vector<int> &a, const vector<int> &b) {
  if (a.empty() || b.empty())
    return {};
  int sz = a.size() + b.size() - 1;
  int n = sz == 1 ? 1 : 1 << (32 - __builtin_clz(sz - 1));
  if (n > fa.size())
    fa.resize(n);
  for (int i = 0; i < n; ++i) {
    int x = i < a.size() ? a[i] : 0;
    int y = i < b.size() ? b[i] : 0;
    fa[i] = cpx(x, y);
  }
  fft(fa, n);
  cpx r(0, -0.5 / n);
  for (int i = 0; i <= (n >> 1); ++i) {
    int j = (n - i) & (n - 1);
    cpx x = (fa[i] + fa[j].conj()) / 2;
    cpx y = (fa[i] - fa[j].conj()) * r;
    fa[i] = x * y;
    if (i != j)
      fa[j] = fa[i].conj();
  }
  fft(fa, n);
  reverse(fa.begin() + 1, fa.begin() + n);
  vector<ll> res(sz);
  for (int i = 0; i < sz; ++i) {
    res[i] = llround(fa[i].r);
  }
  return res;
}
// Accepts Mint or int or ll in range [0..m-1].
template <typename T>
vector<T> multiply(const vector<T> &a, const vector<T> &b, int m,
                   bool forceFast = false) {
  if (forceFast || m < (1 << 15)) {
    vector<int> aa(a.begin(), a.end()), bb(b.begin(), b.end());
    auto res = multiply(aa, bb);
    vector<T> ret(res.size());
    for (int i = 0; i < res.size(); ++i)
      ret[i] = res[i] % m;
    return ret;
  }
  if (a.empty() || b.empty())
    return {};
  int eq = (a == b);
  int sz = a.size() + b.size() - 1;
  int n = sz == 1 ? 1 : 1 << (32 - __builtin_clz(sz - 1));
  if (n > fa.size())
    fa.resize(n);
  for (int i = 0; i < a.size(); ++i) {
    fa[i] = cpx(int(a[i]) & ((1 << 15) - 1), int(a[i]) >> 15);
  }
  fill(fa.begin() + a.size(), fa.begin() + n, cpx(0, 0));
  fft(fa, n);
  if (n > fb.size())
    fb.resize(n);
  if (eq) {
    copy(fa.begin(), fa.begin() + n, fb.begin());
  } else {
    for (int i = 0; i < b.size(); ++i) {
      fb[i] = cpx(int(b[i]) & ((1 << 15) - 1), int(b[i]) >> 15);
    }
    fill(fb.begin() + b.size(), fb.begin() + n, cpx(0, 0));
    fft(fb, n);
  }
  double ratio = 0.5 / n;
  cpx r2(0, -0.5);
  cpx r3(ratio, 0);
  cpx r4(0, -ratio);
  cpx r5(0, 1);
  for (int i = 0; i <= (n >> 1); ++i) {
    int j = (n - i) & (n - 1);
    cpx a1 = (fa[i] + fa[j].conj()) / 2;
    cpx a2 = (fa[i] - fa[j].conj()) * r2;
    cpx b1 = (fb[i] + fb[j].conj()) * r3;
    cpx b2 = (fb[i] - fb[j].conj()) * r4;
    cpx x = a1 * b1;
    cpx y = a2 * b2;
    fa[i] = x + y * r5;
    fb[i] = a1 * b2 + a2 * b1;
    if (i != j) {
      fa[j] = x.conj() + y.conj() * r5;
      fb[j] = fb[i].conj();
    }
  }
  fft(fa, n);
  fft(fb, n);
  reverse(fa.begin() + 1, fa.begin() + n);
  reverse(fb.begin() + 1, fb.begin() + n);
  vector<T> res(sz);
  for (int i = 0; i < sz; ++i) {
    ll aa = llround(fa[i].r);
    ll bb = llround(fb[i].r);
    ll cc = llround(fa[i].i);
    res[i] = (aa + ((bb % m) << 15) + ((cc % m) << 30)) % m;
  }
  return res;
}
/*
vector<Mint> multiply(const vector<Mint>& a, const vector<Mint>& b, bool
forceFast = false) { return multiply(a, b, MOD, forceFast);
}
*/
}; // namespace fft
const int P = 200003;
const int ROOT = 2;
int main() {
  ll v = 1;
  vector<int> pos(P, -1), val(P - 1);
  for (int i = 0; i <= P - 2; ++i) {
    assert(1 <= v && v < P);
    assert(pos[v] == -1);
    pos[v] = i;
    val[i] = v;
    v = v * ROOT % P;
  }
  assert(v == 1);
  vector<int> poly(P - 1);
  int n;
  scanf("%d", &n);
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    int a;
    scanf("%d", &a);
    if (!a)
      continue;
    poly[pos[a]]++;
    ans -= ((ll)a * a) % P;
  }
  vector<ll> res = fft::multiply(poly, poly);
  for (int i = 0; i < res.size(); ++i) {
    ans += res[i] * val[i % (P - 1)];
  }
  assert(ans % 2 == 0);
  ans /= 2;
  printf("%lld\\n", ans);
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define dbv(v)                                                                 \\
  cerr << #v << "=";                                                           \\
  for (auto _x : v)                                                            \\
    cerr << _x << ", ";                                                        \\
  cerr << endl
#define dba(a, n)                                                              \\
  cerr << #a << "=";                                                           \\
  for (int _i = 0; _i < (n); ++_i)                                             \\
    cerr << a[_i] << ", ";                                                     \\
  cerr << endl
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &x) {
  return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
namespace fft {
struct cpx {
  double r, i;
  cpx(double r = 0, double i = 0) : r(r), i(i) {}
  cpx operator+(cpx b) { return {r + b.r, i + b.i}; }
  cpx operator-(cpx b) { return {r - b.r, i - b.i}; }
  cpx operator*(cpx b) { return {r * b.r - i * b.i, r * b.i + i * b.r}; }
  cpx operator/(double b) { return {r / b, i / b}; }
  inline cpx conj() { return {r, -i}; }
};
const double PI = acos(-1);
vector<int> rev(1, 0); // rev has [base] bits
vector<cpx> roots;
int base = 0;
void precompute(int nbase) {
  if (nbase <= base)
    return;
  rev.resize(1 << nbase);
  for (int i = 0; i < (1 << nbase); ++i) {
    rev[i] = rev[i >> 1] >> 1 | ((i & 1) << nbase - 1);
  }
  roots.resize(1 << nbase);
  for (; base < nbase; ++base) {
    int len = 1 << base;
    double angle = 2 * PI / (1 << (base + 1));
    for (int i = 0; i < len; ++i) {
      double cur = angle * i;
      roots[len + i] = cpx(cos(cur), sin(cur));
    }
  }
}
void fft(vector<cpx> &a, int n = -1) {
  if (n == -1)
    n = a.size();
  assert((n & (n - 1)) == 0);
  int zeros = __builtin_ctz(n);
  precompute(zeros);
  int shift = base - zeros;
  for (int i = 0; i < n; ++i) {
    if (i < (rev[i] >> shift))
      swap(a[i], a[rev[i] >> shift]);
  }
  for (int len = 1; len < n; len <<= 1)
    for (int i = 0; i < n; i += 2 * len)
      for (int j = 0; j < len; ++j) {
        cpx u = a[i + j], v = a[i + j + len] * roots[len + j];
        a[i + j] = u + v;
        a[i + j + len] = u - v;
      }
}
vector<cpx> fa, fb;
vector<ll> multiply(const vector<int> &a, const vector<int> &b) {
  if (a.empty() || b.empty())
    return {};
  int sz = a.size() + b.size() - 1;
  int n = sz == 1 ? 1 : 1 << (32 - __builtin_clz(sz - 1));
  if (n > fa.size())
    fa.resize(n);
  for (int i = 0; i < n; ++i) {
    int x = i < a.size() ? a[i] : 0;
    int y = i < b.size() ? b[i] : 0;
    fa[i] = cpx(x, y);
  }
  fft(fa, n);
  cpx r(0, -0.5 / n);
  for (int i = 0; i <= (n >> 1); ++i) {
    int j = (n - i) & (n - 1);
    cpx x = (fa[i] + fa[j].conj()) / 2;
    cpx y = (fa[i] - fa[j].conj()) * r;
    fa[i] = x * y;
    if (i != j)
      fa[j] = fa[i].conj();
  }
  fft(fa, n);
  reverse(fa.begin() + 1, fa.begin() + n);
  vector<ll> res(sz);
  for (int i = 0; i < sz; ++i) {
    res[i] = llround(fa[i].r);
  }
  return res;
}
// Accepts Mint or int or ll in range [0..m-1].
template <typename T>
vector<T> multiply(const vector<T> &a, const vector<T> &b, int m,
                   bool forceFast = false) {
  if (forceFast || m < (1 << 15)) {
    vector<int> aa(a.begin(), a.end()), bb(b.begin(), b.end());
    auto res = multiply(aa, bb);
    vector<T> ret(res.size());
    for (int i = 0; i < res.size(); ++i)
      ret[i] = res[i] % m;
    return ret;
  }
  if (a.empty() || b.empty())
    return {};
  int eq = (a == b);
  int sz = a.size() + b.size() - 1;
  int n = sz == 1 ? 1 : 1 << (32 - __builtin_clz(sz - 1));
  if (n > fa.size())
    fa.resize(n);
  for (int i = 0; i < a.size(); ++i) {
    fa[i] = cpx(int(a[i]) & ((1 << 15) - 1), int(a[i]) >> 15);
  }
  fill(fa.begin() + a.size(), fa.begin() + n, cpx(0, 0));
  fft(fa, n);
  if (n > fb.size())
    fb.resize(n);
  if (eq) {
    copy(fa.begin(), fa.begin() + n, fb.begin());
  } else {
    for (int i = 0; i < b.size(); ++i) {
      fb[i] = cpx(int(b[i]) & ((1 << 15) - 1), int(b[i]) >> 15);
    }
    fill(fb.begin() + b.size(), fb.begin() + n, cpx(0, 0));
    fft(fb, n);
  }
  double ratio = 0.5 / n;
  cpx r2(0, -0.5);
  cpx r3(ratio, 0);
  cpx r4(0, -ratio);
  cpx r5(0, 1);
  for (int i = 0; i <= (n >> 1); ++i) {
    int j = (n - i) & (n - 1);
    cpx a1 = (fa[i] + fa[j].conj()) / 2;
    cpx a2 = (fa[i] - fa[j].conj()) * r2;
    cpx b1 = (fb[i] + fb[j].conj()) * r3;
    cpx b2 = (fb[i] - fb[j].conj()) * r4;
    cpx x = a1 * b1;
    cpx y = a2 * b2;
    fa[i] = x + y * r5;
    fb[i] = a1 * b2 + a2 * b1;
    if (i != j) {
      fa[j] = x.conj() + y.conj() * r5;
      fb[j] = fb[i].conj();
    }
  }
  fft(fa, n);
  fft(fb, n);
  reverse(fa.begin() + 1, fa.begin() + n);
  reverse(fb.begin() + 1, fb.begin() + n);
  vector<T> res(sz);
  for (int i = 0; i < sz; ++i) {
    ll aa = llround(fa[i].r);
    ll bb = llround(fb[i].r);
    ll cc = llround(fa[i].i);
    res[i] = (aa + ((bb % m) << 15) + ((cc % m) << 30)) % m;
  }
  return res;
}
/*
vector<Mint> multiply(const vector<Mint>& a, const vector<Mint>& b, bool
forceFast = false) { return multiply(a, b, MOD, forceFast);
}
*/
}; // namespace fft
const int P = 200003;
const int ROOT = 2;
int main() {
  ll v = 1;
  vector<int> pos(P, -1), val(P - 1);
  for (int i = 0; i <= P - 2; ++i) {
    assert(1 <= v && v < P);
    assert(pos[v] == -1);
    pos[v] = i;
    val[i] = v;
    v = v * ROOT % P;
  }
  assert(v == 1);
  vector<int> poly(P - 1);
  int n;
  scanf("%d", &n);
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    int a;
    scanf("%d", &a);
    poly[pos[a]]++;
    ans -= ((ll)a * a) % P;
  }
  auto res = fft::multiply(poly, poly);
  for (int i = 0; i < res.size(); ++i) {
    ans += res[i] * val[i % (P - 1)];
  }
  assert(ans % 2 == 0);
  ans /= 2;
  printf("%lld\\n", ans);
}