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

#include <bits/stdc++.h>
#define ll long long
#define uint unsigned int
#define ull unsigned long long
#define inf 1010000000
#define infll 1010000000000000000ll
#define vi vector<int>
#define vll vector<ll>
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define mpr make_pair
#define fi first
#define se second
#define pq priority_queue<int>
#define pqll priority_queue<ll>
#define up(x, y) (x < (y) ? x = (y) : 0)
#define dn(x, y) (x > (y) ? x = (y) : 0)
#define ad(x, y) (x = (x + (y)) % mod)
#define cbn(x, y) ((ll)fac[x] * inv[y] % mod * inv[(x) - (y)] % mod)
#define P 200003
#define N 600009
#define pi acos(-1.)
using namespace std;

int read();
ll readll();

int n, m, a[N], pos[N], id[N];
struct cpx {
  double r, i;
} b[N];
cpx operator+(cpx x, cpx y) {
  x.r += y.r;
  x.i += y.i;
  return x;
}
cpx operator-(cpx x, cpx y) {
  x.r -= y.r;
  x.i -= y.i;
  return x;
}
cpx operator*(cpx x, cpx y) {
  cpx t;
  t.r = x.r * y.r - x.i * y.i;
  t.i = x.r * y.i + x.i * y.r;
  return t;
}
void dft(cpx *a, int flag) {
  int i, j, k;
  cpx w, wn, u, v;
  for (i = 0; i < m; i++)
    if (i < pos[i])
      swap(a[i], a[pos[i]]);
  for (k = 1; k < m; k <<= 1) {
    wn.r = cos(pi * flag / k);
    wn.i = sin(pi * flag / k);
    for (i = 0; i < m; i += (k << 1)) {
      w.r = 1;
      w.i = 0;
      for (j = i; j < i + k; j++) {
        u = a[j];
        v = a[j + k] * w;
        a[j] = u + v;
        a[j + k] = u - v;
        w = w * wn;
      }
    }
  }
  if (flag < 0)
    for (i = 0; i < m; i++)
      a[i].r /= m;
}
int ksm(int x, int y) {
  int z = 1;
  for (; y; y >>= 1, x = (ll)x * x % P)
    if (y & 1)
      z = (ll)z * x % P;
  return z;
}
bool check(int x) {
  int i;
  for (i = 2; i * i < P; i++)
    if ((P - 1) % i == 0) {
      if (ksm(x, i) == 1)
        return 0;
      if (ksm(x, (P - 1) / i) == 1)
        return 0;
    }
  return 1;
}
int main() {
  scanf("%d", &n);
  int i, j;
  for (i = 3;; i++)
    if (check(i))
      break;
  int g = i;
  //	cerr<<g<<'\\n';
  for (i = 0, j = 1; i < P - 1; i++, j = (ll)j * g % P) {
    //	assert(id[j]==0);
    id[j] = i;
  }
  id[0] = P - 1;
  for (i = 1; i <= n; i++)
    a[id[read()]]++;
  a[P - 1] = 0;
  m = 1 << 19;
  //	cerr<<m<<'\\n';
  for (i = 0; i < m; i++)
    pos[i] = pos[i >> 1] >> 1 | ((i & 1) ? (m >> 1) : 0);
  //	for (i=0; i<m; i++) cerr<<a[i]<<' ';cerr<<'\\n';
  for (i = 0; i < m; i++)
    b[i].r = a[i];
  dft(b, 1);
  for (i = 0; i < m; i++)
    b[i] = b[i] * b[i];
  dft(b, -1);
  //	for (i=0; i<m; i++) cerr<<b[i].r<<' '<<b[i].i<<"	"<<'\\n';
  ll ans = 0;
  for (i = 0; i < m; i++)
    if (b[i].r > 1e-7) {
      //	cerr<<"??"<<ksm(g,i)<<' '<<b[i].r<<'\\n';
      ans += ksm(g, i) * ((ll)round(b[i].r));
    }
  //	cerr<<"----"<<'\\n';
  for (i = 0; i < P - 1; i++)
    if (a[i]) {
      int x = ksm(g, i);
      //	cerr<<i<<' '<<x<<'\\n';
      ans -= (ll)x * x % P * a[i];
    }
  // assert(ans%2==0);
  printf("%lld\\n", ans / 2);
  return 0;
}

int read() {
  int x = 0;
  char ch = getchar();
  bool flag = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      flag = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return flag ? -x : x;
}
ll readll() {
  ll x = 0;
  char ch = getchar();
  bool flag = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      flag = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return flag ? -x : x;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define uint unsigned int
#define ull unsigned long long
#define inf 1010000000
#define infll 1010000000000000000ll
#define vi vector<int>
#define vll vector<ll>
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define mpr make_pair
#define fi first
#define se second
#define pq priority_queue<int>
#define pqll priority_queue<ll>
#define up(x, y) (x < (y) ? x = (y) : 0)
#define dn(x, y) (x > (y) ? x = (y) : 0)
#define ad(x, y) (x = (x + (y)) % mod)
#define cbn(x, y) ((ll)fac[x] * inv[y] % mod * inv[(x) - (y)] % mod)
#define P 200003
#define N 600009
#define pi acos(-1.)
using namespace std;

int read();
ll readll();

int n, m, a[N], pos[N], id[N];
struct cpx {
  double r, i;
} b[N];
cpx operator+(cpx x, cpx y) {
  x.r += y.r;
  x.i += y.i;
  return x;
}
cpx operator-(cpx x, cpx y) {
  x.r -= y.r;
  x.i -= y.i;
  return x;
}
cpx operator*(cpx x, cpx y) {
  cpx t;
  t.r = x.r * y.r - x.i * y.i;
  t.i = x.r * y.i + x.i * y.r;
  return t;
}
void dft(cpx *a, int flag) {
  int i, j, k;
  cpx w, wn, u, v;
  for (i = 0; i < m; i++)
    if (i < pos[i])
      swap(a[i], a[pos[i]]);
  for (k = 1; k < m; k <<= 1) {
    wn.r = cos(pi * flag / k);
    wn.i = sin(pi * flag / k);
    for (i = 0; i < m; i += (k << 1)) {
      w.r = 1;
      w.i = 0;
      for (j = i; j < i + k; j++) {
        u = a[j];
        v = a[j + k] * w;
        a[j] = u + v;
        a[j + k] = u - v;
        w = w * wn;
      }
    }
  }
  if (flag < 0)
    for (i = 0; i < m; i++)
      a[i].r /= m;
}
int ksm(int x, int y) {
  int z = 1;
  for (; y; y >>= 1, x = (ll)x * x % P)
    if (y & 1)
      z = (ll)z * x % P;
  return z;
}
bool check(int x) {
  int i;
  for (i = 2; i * i < P; i++)
    if ((P - 1) % i == 0) {
      if (ksm(x, i) == 1)
        return 0;
      if (ksm(x, (P - 1) / i) == 1)
        return 0;
    }
  return 1;
}
int main() {
  scanf("%d", &n);
  int i, j;
  for (i = 3;; i++)
    if (check(i))
      break;
  int g = i;
  //	cerr<<g<<'\\n';
  for (i = 0, j = 1; i < P - 1; i++, j = (ll)j * g % P) {
    assert(id[j] == 0);
    id[j] = i;
  }
  id[0] = P - 1;
  for (i = 1; i <= n; i++)
    a[id[read()]]++;
  a[P - 1] = 0;
  m = 1 << 19;
  //	cerr<<m<<'\\n';
  for (i = 0; i < m; i++)
    pos[i] = pos[i >> 1] >> 1 | ((i & 1) ? (m >> 1) : 0);
  //	for (i=0; i<m; i++) cerr<<a[i]<<' ';cerr<<'\\n';
  for (i = 0; i < m; i++)
    b[i].r = a[i];
  dft(b, 1);
  for (i = 0; i < m; i++)
    b[i] = b[i] * b[i];
  dft(b, -1);
  //	for (i=0; i<m; i++) cerr<<b[i].r<<' '<<b[i].i<<"	"<<'\\n';
  ll ans = 0;
  for (i = 0; i < m; i++)
    if (b[i].r > 1e-7) {
      //	cerr<<"??"<<ksm(g,i)<<' '<<b[i].r<<'\\n';
      ans += ksm(g, i) * ((ll)round(b[i].r));
    }
  //	cerr<<"----"<<'\\n';
  for (i = 0; i < P - 1; i++)
    if (a[i]) {
      int x = ksm(g, i);
      //	cerr<<i<<' '<<x<<'\\n';
      ans -= (ll)x * x % P * a[i];
    }
  assert(ans % 2 == 0);
  printf("%lld\\n", ans / 2);
  return 0;
}

int read() {
  int x = 0;
  char ch = getchar();
  bool flag = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      flag = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return flag ? -x : x;
}
ll readll() {
  ll x = 0;
  char ch = getchar();
  bool flag = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      flag = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return flag ? -x : x;
}