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

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <fstream>
#include <iostream>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define mp make_pair

using namespace std;

typedef long long int64;
typedef vector<int> vi;
typedef pair<int, int> ii;

constexpr int64 MODULO = 200003;
constexpr int MAXN = 1 << 19;
typedef long double ld;
const ld PI = 4 * atanl(1);

typedef complex<ld> base;
int rev[MAXN];
base wlen_pw[MAXN];

void fft(base *a, int n, bool invert) {
  for (int i = 0; i < n; ++i)
    if (i < rev[i])
      swap(a[i], a[rev[i]]);

  for (int len = 2; len <= n; len <<= 1) {
    ld ang = 2 * PI / len * (invert ? -1 : +1);
    int len2 = len >> 1;

    base wlen(cos(ang), sin(ang));
    wlen_pw[0] = base(1, 0);
    for (int i = 1; i < len2; ++i)
      wlen_pw[i] = wlen_pw[i - 1] * wlen;

    for (int i = 0; i < n; i += len) {
      base t, *pu = a + i, *pv = a + i + len2, *pu_end = a + i + len2,
              *pw = wlen_pw;
      for (; pu != pu_end; ++pu, ++pv, ++pw) {
        t = *pv * *pw;
        *pv = *pu - t;
        *pu += t;
      }
    }
  }

  if (invert)
    for (int i = 0; i < n; ++i)
      a[i] /= n;
}

void calc_rev(int n, int log_n) {
  for (int i = 0; i < n; ++i) {
    rev[i] = 0;
    for (int j = 0; j < log_n; ++j)
      if (i & (1 << j))
        rev[i] |= 1 << (log_n - 1 - j);
  }
}

void multiply(const vector<int64> &a, const vector<int64> &b,
              vector<int64> &res) {
  vector<base> fa(a.begin(), a.end()), fb(b.begin(), b.end());
  size_t n = 1;
  int log_n = 0;
  while (n < max(a.size(), b.size())) {
    n <<= 1;
    ++log_n;
  }
  n <<= 1;
  ++log_n;
  calc_rev(n, log_n);
  fa.resize(n), fb.resize(n);

  fft(fa.data(), fa.size(), false), fft(fb.data(), fb.size(), false);
  for (size_t i = 0; i < n; ++i)
    fa[i] *= fb[i];
  fft(fa.data(), fa.size(), true);

  res.resize(n);
  for (size_t i = 0; i < n; ++i)
    res[i] = int64(fa[i].real() + 0.5);
}

class Task {
public:
  void solveOne(istream &in, ostream &out) {
    int n;
    in >> n;
    int64 g = 2;
    vector<int> logs(MODULO, -1);
    vector<int64> pows(MODULO - 1, -1);
    while (true) {
      int64 prod = 1;
      int pow = 0;
      do {
        logs[(int)prod] = pow;
        pows[pow] = prod;
        prod = prod * g % MODULO;
        ++pow;
      } while (prod != 1);
      if (pow == MODULO - 1) {
        break;
      }
    }
    vector<int64> counts(MODULO);
    for (int i = 0; i < n; ++i) {
      int a;
      in >> a;
      if (a > 0) {
        ++counts[logs[a]];
      }
    }
    vector<int64> res;
    multiply(counts, counts, res);
    int64 sum = 0;
    for (int i = 0; i < res.size(); ++i) {
      int64 val = pows[i % (MODULO - 1)];
      int64 count = res[i];
      sum = (sum + val * count);
    }
    for (int i = 0; i < counts.size(); ++i) {
      int64 val = pows[i * 2 % (MODULO - 1)];
      int64 count = counts[i];
      sum = (sum - val * count);
    }
    assert(sum % 2 == 0);
    sum /= 2;
    out << sum << "\\n";
  }

  void solve(std::istream &in, std::ostream &out) {
    int nt = 1;
    for (int it = 0; it < nt; ++it) {
      solveOne(in, out);
    }
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  Task solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Error: 0

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <fstream>
#include <iostream>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define mp make_pair

using namespace std;

typedef long long int64;
typedef vector<int> vi;
typedef pair<int, int> ii;

constexpr int64 MODULO = 200003;
constexpr int MAXN = 1 << 19;
const double PI = 4 * atan(1);

typedef complex<double> base;
int rev[MAXN];
base wlen_pw[MAXN];

void fft(base *a, int n, bool invert) {
  for (int i = 0; i < n; ++i)
    if (i < rev[i])
      swap(a[i], a[rev[i]]);

  for (int len = 2; len <= n; len <<= 1) {
    double ang = 2 * PI / len * (invert ? -1 : +1);
    int len2 = len >> 1;

    base wlen(cos(ang), sin(ang));
    wlen_pw[0] = base(1, 0);
    for (int i = 1; i < len2; ++i)
      wlen_pw[i] = wlen_pw[i - 1] * wlen;

    for (int i = 0; i < n; i += len) {
      base t, *pu = a + i, *pv = a + i + len2, *pu_end = a + i + len2,
              *pw = wlen_pw;
      for (; pu != pu_end; ++pu, ++pv, ++pw) {
        t = *pv * *pw;
        *pv = *pu - t;
        *pu += t;
      }
    }
  }

  if (invert)
    for (int i = 0; i < n; ++i)
      a[i] /= n;
}

void calc_rev(int n, int log_n) {
  for (int i = 0; i < n; ++i) {
    rev[i] = 0;
    for (int j = 0; j < log_n; ++j)
      if (i & (1 << j))
        rev[i] |= 1 << (log_n - 1 - j);
  }
}

void multiply(const vector<int64> &a, const vector<int64> &b,
              vector<int64> &res) {
  vector<base> fa(a.begin(), a.end()), fb(b.begin(), b.end());
  size_t n = 1;
  int log_n = 0;
  while (n < max(a.size(), b.size())) {
    n <<= 1;
    ++log_n;
  }
  n <<= 1;
  ++log_n;
  calc_rev(n, log_n);
  fa.resize(n), fb.resize(n);

  fft(fa.data(), fa.size(), false), fft(fb.data(), fb.size(), false);
  for (size_t i = 0; i < n; ++i)
    fa[i] *= fb[i];
  fft(fa.data(), fa.size(), true);

  res.resize(n);
  for (size_t i = 0; i < n; ++i)
    res[i] = int64(fa[i].real() + 0.5);
}

class Task {
public:
  void solveOne(istream &in, ostream &out) {
    int n;
    in >> n;
    int64 g = 2;
    vector<int> logs(MODULO, -1);
    vector<int64> pows(MODULO - 1, -1);
    while (true) {
      int64 prod = 1;
      int pow = 0;
      do {
        logs[(int)prod] = pow;
        pows[pow] = prod;
        prod = prod * g % MODULO;
        ++pow;
      } while (prod != 1);
      if (pow == MODULO - 1) {
        break;
      }
    }
    vector<int64> counts(MODULO);
    for (int i = 0; i < n; ++i) {
      int a;
      in >> a;
      if (a > 0) {
        ++counts[logs[a]];
      }
    }
    vector<int64> res;
    multiply(counts, counts, res);
    int64 sum = 0;
    for (int i = 0; i < res.size(); ++i) {
      int64 val = pows[i % (MODULO - 1)];
      int64 count = res[i];
      sum = (sum + val * count);
    }
    for (int i = 0; i < counts.size(); ++i) {
      int64 val = pows[i * 2 % (MODULO - 1)];
      int64 count = counts[i];
      sum = (sum - val * count);
    }
    assert(sum % 2 == 0);
    sum /= 2;
    out << sum << "\\n";
  }

  void solve(std::istream &in, std::ostream &out) {
    int nt = 1;
    for (int it = 0; it < nt; ++it) {
      solveOne(in, out);
    }
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  Task solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}