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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ull = unsigned long long;
using ld = long double;
template <typename T> void _(const char *s, T h) {
  cerr << s << "=" << h << "\\n";
}
template <typename T, typename... Ts> void _(const char *s, T h, Ts... t) {
  int b = 0;
  while (((b += *s == '(') -= *s == ')') != 0 || *s != ',')
    cerr << *s++;
  cerr << "=" << h << ",";
  _(s + 1, t...);
} // break continue pop_back 998244353
#define int ll
#define pii pll
#define f first
#define s second
#define pb emplace_back
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define sz(a) ((int)(a).size())
#define sqr(x) ((x) * (x))
struct init {
  init() {
    cin.tie(0);
    iostream::sync_with_stdio(0);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(5);
  }
  ~init() {
#ifdef LOCAL
#define dbg(...) _(#__VA_ARGS__, __VA_ARGS__)
    cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC << "s.\\n";
#else
#define dbg(...)
#endif
  }
} init;
template <typename T, typename U> void upx(T &x, U y) {
  if (x < y)
    x = y;
}
template <typename T, typename U> void upn(T &x, U y) {
  if (x > y)
    x = y;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int D = 4, dx[] = {+1, 0, -1, 0}, dy[] = {0, +1, 0, -1};
const int M = 200003;
namespace fft {
typedef double dbl;
struct cpx {
  dbl x, y;
  cpx() { x = y = 0; }
  cpx(dbl x_, dbl y_) : x(x_), y(y_) {}
};
inline cpx operator+(cpx a, cpx b) { return cpx(a.x + b.x, a.y + b.y); }
inline cpx operator-(cpx a, cpx b) { return cpx(a.x - b.x, a.y - b.y); }
inline cpx operator*(cpx a, cpx b) {
  return cpx(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
inline cpx conj(cpx a) { return cpx(a.x, -a.y); }
int32_t base = 1;
vector<cpx> roots = {{0, 0}, {1, 0}};
vector<int32_t> rev = {0, 1};
const dbl PI = static_cast<dbl>(acosl(-1.0));
void ensure_base(int32_t nbase) {
  if (nbase <= base)
    return;
  rev.resize(1 << nbase);
  for (int32_t i = 0; i < (1 << nbase); i++) {
    rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
  }
  roots.resize(1 << nbase);
  while (base < nbase) {
    dbl angle = 2 * PI / (1 << (base + 1));
    for (int32_t i = 1 << (base - 1); i < (1 << base); i++) {
      roots[i << 1] = roots[i];
      dbl angle_i = angle * (2 * i + 1 - (1 << base));
      roots[(i << 1) + 1] = cpx(cos(angle_i), sin(angle_i));
    }
    base++;
  }
}
void fft(vector<cpx> &a, int32_t n = -1) {
  if (n == -1) {
    n = (int32_t)a.size();
  }
  assert((n & (n - 1)) == 0);
  int32_t zeros = __builtin_ctz(n);
  ensure_base(zeros);
  int32_t shift = base - zeros;
  for (int32_t i = 0; i < n; i++) {
    if (i < (rev[i] >> shift)) {
      swap(a[i], a[rev[i] >> shift]);
    }
  }
  for (int32_t k = 1; k < n; k <<= 1) {
    for (int32_t i = 0; i < n; i += 2 * k) {
      for (int32_t j = 0; j < k; j++) {
        cpx z = a[i + j + k] * roots[j + k];
        a[i + j + k] = a[i + j] - z;
        a[i + j] = a[i + j] + z;
      }
    }
  }
}
vector<cpx> fa, fb;
vector<int64_t> square(const vector<int> &a) {
  if (a.empty()) {
    return {};
  }
  int32_t need = (int32_t)a.size() + (int32_t)a.size() - 1;
  int32_t nbase = 1;
  while ((1 << nbase) < need)
    nbase++;
  ensure_base(nbase);
  int32_t z = 1 << nbase;
  if ((z >> 1) > (int32_t)fa.size())
    fa.resize(z >> 1);
  for (int32_t i = 0; i < (z >> 1); i++) {
    int32_t x = (2 * i < (int32_t)a.size() ? a[2 * i] : 0);
    int32_t y = (2 * i + 1 < (int32_t)a.size() ? a[2 * i + 1] : 0);
    fa[i] = cpx(x, y);
  }
  fft(fa, z >> 1);
  cpx r(1.0 / (z >> 1), 0.0);
  for (int32_t i = 0; i <= (z >> 2); i++) {
    int32_t j = ((z >> 1) - i) & ((z >> 1) - 1);
    cpx fe = (fa[i] + conj(fa[j])) * cpx(0.5, 0);
    cpx fo = (fa[i] - conj(fa[j])) * cpx(0, -0.5);
    cpx aux = fe * fe + fo * fo * roots[(z >> 1) + i] * roots[(z >> 1) + i];
    cpx tmp = fe * fo;
    fa[i] = r * (conj(aux) + cpx(0, 2) * conj(tmp));
    fa[j] = r * (aux + cpx(0, 2) * tmp);
  }
  fft(fa, z >> 1);
  vector<int64_t> res(need);
  for (int32_t i = 0; i < need; i++) {
    res[i] = llround(i % 2 == 0 ? fa[i >> 1].x : fa[i >> 1].y);
  }
  return res;
}
vector<int64_t> multiply(const vector<int> &a, const vector<int> &b) {
  if (a.empty() || b.empty()) {
    return {};
  }
  if (a == b) {
    return square(a);
  }
  int32_t need = (int32_t)a.size() + (int32_t)b.size() - 1;
  int32_t nbase = 1;
  while ((1 << nbase) < need)
    nbase++;
  ensure_base(nbase);
  int32_t z = 1 << nbase;
  if (z > (int32_t)fa.size()) {
    fa.resize(z);
  }
  for (int32_t i = 0; i < z; i++) {
    int32_t x = (i < sz(a) ? a[i] : 0);
    int32_t y = (i < sz(b) ? b[i] : 0);
    fa[i] = cpx(x, y);
  }
  fft(fa, z);
  cpx r(0, -0.25 / (z >> 1));
  for (int32_t i = 0; i <= (z >> 1); i++) {
    int32_t j = (z - i) & (z - 1);
    cpx o = (fa[j] * fa[j] - conj(fa[i] * fa[i])) * r;
    fa[j] = (fa[i] * fa[i] - conj(fa[j] * fa[j])) * r;
    fa[i] = o;
  }
  for (int32_t i = 0; i < (z >> 1); i++) {
    cpx A0 = (fa[i] + fa[i + (z >> 1)]) * cpx(0.5, 0);
    cpx A1 = (fa[i] - fa[i + (z >> 1)]) * cpx(0.5, 0) * roots[(z >> 1) + i];
    fa[i] = A0 + A1 * cpx(0, 1);
  }
  fft(fa, z >> 1);
  vector<int64_t> res(need);
  for (int32_t i = 0; i < need; i++) {
    res[i] = llround(i % 2 == 0 ? fa[i >> 1].x : fa[i >> 1].y);
  }
  return res;
}
vector<int> multiply_mod(const vector<int> &a, const vector<int> &b, int m) {
  if (a.empty() || b.empty()) {
    return {};
  }
  int eq = (a.size() == b.size() && a == b);
  int need = sz(a) + sz(b) - 1;
  int nbase = 0;
  while ((1 << nbase) < need)
    nbase++;
  ensure_base(nbase);
  int z = 1 << nbase;
  if (z > sz(fa)) {
    fa.resize(z);
  }
  for (int i = 0; i < sz(a); i++) {
    int x = (a[i] % m + m) % m;
    fa[i] = cpx(x & ((1 << 15) - 1), x >> 15);
  }
  fill(fa.begin() + sz(a), fa.begin() + z, cpx{0, 0});
  fft(fa, z);
  if (z > sz(fb)) {
    fb.resize(z);
  }
  if (eq) {
    copy(fa.begin(), fa.begin() + z, fb.begin());
  } else {
    for (int i = 0; i < sz(b); i++) {
      int x = (b[i] % m + m) % m;
      fb[i] = cpx(x & ((1 << 15) - 1), x >> 15);
    }
    fill(fb.begin() + b.size(), fb.begin() + z, cpx{0, 0});
    fft(fb, z);
  }
  dbl ratio = 0.25 / z;
  cpx r2(0, -1);
  cpx r3(ratio, 0);
  cpx r4(0, -ratio);
  cpx r5(0, 1);
  for (int i = 0; i <= (z >> 1); i++) {
    int j = (z - i) & (z - 1);
    cpx a1 = (fa[i] + conj(fa[j]));
    cpx a2 = (fa[i] - conj(fa[j])) * r2;
    cpx b1 = (fb[i] + conj(fb[j])) * r3;
    cpx b2 = (fb[i] - conj(fb[j])) * r4;
    if (i != j) {
      cpx c1 = (fa[j] + conj(fa[i]));
      cpx c2 = (fa[j] - conj(fa[i])) * r2;
      cpx d1 = (fb[j] + conj(fb[i])) * r3;
      cpx d2 = (fb[j] - conj(fb[i])) * r4;
      fa[i] = c1 * d1 + c2 * d2 * r5;
      fb[i] = c1 * d2 + c2 * d1;
    }
    fa[j] = a1 * b1 + a2 * b2 * r5;
    fb[j] = a1 * b2 + a2 * b1;
  }
  fft(fa, z);
  fft(fb, z);
  vector<int> res(need);
  for (int i = 0; i < need; i++) {
    int64_t aa = llround(fa[i].x);
    int64_t bb = llround(fb[i].x);
    int64_t cc = llround(fa[i].y);
    res[i] = static_cast<int>((aa + ((bb % m) << 15) + ((cc % m) << 30)) % m);
  }
  return res;
}
}; // namespace fft
int kto[M];
int a[M], c[M];
int32_t main() {
  int n;
  cin >> n;
  forn(i, n) {
    cin >> a[i];
    c[a[i]]++;
  }
  int p = 1;
  vector<int> x;
  forn(i, M - 1) {
    kto[i] = p;
    x.pb(c[p]);
    (p *= 2) %= M;
  }
  auto res = fft::multiply(x, x);
  int ans = 0;
  forn(i, sz(res)) { ans += res[i] * kto[i % (M - 1)]; }
  forn(i, n) ans -= (a[i] * a[i]) % M;
  ans /= 2;
  cout << ans << '\\n';
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ull = unsigned long long;
using ld = long double;
template <typename T> void _(const char *s, T h) {
  cerr << s << " = " << h << "\\n";
}
template <typename T, typename... Ts> void _(const char *s, T h, Ts... t) {
  int b = 0;
  while (((b += *s == '(') -= *s == ')') != 0 || *s != ',')
    cerr << *s++;
  cerr << " = " << h << ",";
  _(s + 1, t...);
} // break continue pop_back 998244353
#define int ll
#define pii pll
#define f first
#define s second
#define pb emplace_back
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define sz(a) ((int)(a).size())
#define sqr(x) ((x) * (x))
struct init {
  init() {
    cin.tie(0);
    iostream::sync_with_stdio(0);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(5);
  }
  ~init() {
#ifdef LOCAL
#define dbg(...) _(#__VA_ARGS__, __VA_ARGS__)
    cerr << "Time elapsed: " << (double)clock() / CLOCKS_PER_SEC << "s.\\n";
#else
#define dbg(...)
#endif
  }
} init;
template <typename T, typename U> void upx(T &x, U y) {
  if (x < y)
    x = y;
}
template <typename T, typename U> void upn(T &x, U y) {
  if (x > y)
    x = y;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int D = 4, dx[] = {+1, 0, -1, 0}, dy[] = {0, +1, 0, -1};
const int M = 200003;
struct cmplx {
  double x, y;
  cmplx() = default;
  cmplx(double _x) : x(_x), y(0) {}
  cmplx(double _x, double _y) : x(_x), y(_y) {}
  cmplx conj() const { return cmplx(x, -y); }
};
cmplx operator+(const cmplx &a, const cmplx &b) {
  return cmplx(a.x + b.x, a.y + b.y);
}
cmplx operator-(const cmplx &a, const cmplx &b) {
  return cmplx(a.x - b.x, a.y - b.y);
}
cmplx operator*(const cmplx &a, const cmplx &b) {
  return cmplx(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
cmplx &operator*=(cmplx &a, const cmplx &b) { return a = (a * b); }
cmplx operator/(const cmplx &a, const double &b) {
  return cmplx(a.x / b, a.y / b);
}
const double PI = 3.14159265358979323846;
const int LOGN = 19, N = 1 << LOGN;
cmplx w[LOGN][N];
int rv[LOGN][N];
struct fft_init {
  fft_init() {
    forn(st, LOGN) {
      forn(k, 1 << st) {
        double ang = PI / (1 << st) * k;
        w[st][k] = cmplx(cos(ang), sin(ang));
      }
      if (!st)
        continue;
      int h = (1 << (st - 1));
      forn(k, 1 << st) rv[st][k] = (rv[st - 1][k & (h - 1)] << 1) | (k >= h);
    }
  }
} fft_init;
inline void fft(vector<cmplx> &a, bool inv) {
  int ln = 0;
  int n = a.size();
  while ((1 << ln) < n)
    ++ln;
  assert(ln <= LOGN);
  n = 1 << ln;
  a.resize(n);
  forn(i, n) {
    int ni = rv[ln][i];
    if (i < ni)
      swap(a[i], a[ni]);
  }
  for (int st = 0; (1 << st) < n; ++st) {
    int len = 1 << st;
    for (int k = 0; k < n; k += (len << 1)) {
      for (int pos = k; pos < k + len; ++pos) {
        cmplx l = a[pos],
              r = a[pos + len] * (inv ? w[st][pos - k].conj() : w[st][pos - k]);
        a[pos] = l + r;
        a[pos + len] = l - r;
      }
    }
  }
  if (inv)
    forn(i, n) a[i] = a[i] / n;
}
vector<int> multiply(const vector<cmplx> &a, const vector<cmplx> &b) {
  int n = 1, sza = a.size(), szb = b.size();
  while (n < sza + szb)
    n *= 2;
  auto aa = a;
  auto bb = b;
  aa.resize(n);
  bb.resize(n);
  fft(aa, 0);
  fft(bb, 0);
  forn(i, n) aa[i] *= bb[i];
  fft(aa, 1);
  vector<int> c(n);
  forn(i, n) c[i] = int(aa[i].x + 0.5);
  return c;
}
int kto[N];
int a[N], c[N];
int32_t main() {
  int n;
  cin >> n;
  forn(i, n) {
    cin >> a[i];
    c[a[i]]++;
  }
  vector<cmplx> x;
  int p = 2;
  forn(i, M - 1) {
    kto[i] = p;
    x.pb(c[p]);
    (p *= 2) %= M;
  }
  auto y = x;
  reverse(y.begin(), y.end());
  auto res = multiply(x, y);
  int ans = 0;
  forn(i, sz(res)) { ans += res[i] * kto[i % (M - 1)]; }
  cout << ans << '\\n';
  return 0;
}