Problem p02477 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define maxn 400010
#define ll long long
const double pi = acos(-1);
typedef complex<double> cp;
int n, rev[maxn];
cp omega[maxn], iomega[maxn];
void init(int N) {
  n = N;
  for (int i = 0; i < n; i++) {
    omega[i] = cp(cos(2 * pi / n * i), sin(2 * pi / n * i));
    iomega[i] = conj(omega[i]);
  }
  int k = log2(n);
  for (int i = 0; i < n; i++) {
    int t = 0;
    for (int j = 0; j < k; j++)
      if (i & (1 << j))
        t |= (1 << (k - j - 1));
    rev[i] = t;
  }
}
void transform(vector<cp> &a, cp *omega) {
  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) {
    int mid = len >> 1;
    for (int j = 0; j < n; j += len)
      for (int i = 0; i < mid; i++) {
        cp t = omega[n / len * i] * a[j + mid + i];
        a[j + mid + i] = a[j + i] - t;
        a[j + i] += t;
      }
  }
}
void fft(vector<cp> &a) { transform(a, omega); }
void ifft(vector<cp> &a) {
  transform(a, iomega);
  for (int i = 0; i < n; i++)
    a[i] /= n;
}
vector<ll> s, a, b;
void mul() {
  int n = 1;
  int n1 = (int)a.size(), n2 = (int)b.size();
  while (n < n1 + n2)
    n <<= 1;
  vector<cp> tmp_a(n), tmp_b(n);
  for (int i = 0; i < n1; i++)
    tmp_a[i] = cp(a[i], 0);
  for (int i = 0; i < n2; i++)
    tmp_b[i] = cp(b[i], 0);
  init(n);
  fft(tmp_a);
  fft(tmp_b);
  for (int i = 0; i < n; i++)
    tmp_a[i] *= tmp_b[i];
  ifft(tmp_a);
  s.resize(n);
  for (int i = 0; i < n; i++)
    s[i] = round(tmp_a[i].real());
  while ((int)s.size() > 1 && s.back() == 0)
    s.pop_back();
  return;
}
int main() {
  char c[maxn];
  bool flag = 0;
  scanf("%s", c);
  int n = strlen(c);
  if (c[0] == '-')
    flag ^= 1;
  for (int i = 0; i < n; i++) {
    if (c[i] != '-')
      a.push_back(c[i] - '0');
  }
  reverse(a.begin(), a.end());
  scanf("%s", c);
  if (c[0] == '-')
    flag ^= 1;
  n = strlen(c);
  for (int i = 0; i < n; i++) {
    if (c[i] != '-')
      b.push_back(c[i] - '0');
  }
  reverse(b.begin(), b.end());
  vector<int> ans;
  mul();
  n = (int)s.size();
  int cur = 0;
  for (int i = 0; i < n; i++) {
    cur = cur / 10 + s[i];
    ans.push_back(cur % 10);
  }
  cur /= 10;
  if (cur)
    ans.push_back(cur);
  reverse(ans.begin(), ans.end());
  if (!flag || ((int)ans.size() == 1 && ans[0] == 0)) {
    for (int i = 0; i < (int)ans.size(); i++)
      printf("%d", ans[i]);
    puts("");
  } else {
    putchar('-');
    for (int i = 0; i < (int)ans.size(); i++)
      printf("%d", ans[i]);
    puts("");
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define maxn 800010
#define ll long long
const double pi = acos(-1);
typedef complex<double> cp;
int n, rev[maxn];
cp omega[maxn], iomega[maxn];
void init(int N) {
  n = N;
  for (int i = 0; i < n; i++) {
    omega[i] = cp(cos(2 * pi / n * i), sin(2 * pi / n * i));
    iomega[i] = conj(omega[i]);
  }
  int k = log2(n);
  for (int i = 0; i < n; i++) {
    int t = 0;
    for (int j = 0; j < k; j++)
      if (i & (1 << j))
        t |= (1 << (k - j - 1));
    rev[i] = t;
  }
}
void transform(vector<cp> &a, cp *omega) {
  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) {
    int mid = len >> 1;
    for (int j = 0; j < n; j += len)
      for (int i = 0; i < mid; i++) {
        cp t = omega[n / len * i] * a[j + mid + i];
        a[j + mid + i] = a[j + i] - t;
        a[j + i] += t;
      }
  }
}
void fft(vector<cp> &a) { transform(a, omega); }
void ifft(vector<cp> &a) {
  transform(a, iomega);
  for (int i = 0; i < n; i++)
    a[i] /= n;
}
vector<ll> s, a, b;
void mul() {
  int n = 1;
  int n1 = (int)a.size(), n2 = (int)b.size();
  while (n < n1 + n2)
    n <<= 1;
  vector<cp> tmp_a(n), tmp_b(n);
  for (int i = 0; i < n1; i++)
    tmp_a[i] = cp(a[i], 0);
  for (int i = 0; i < n2; i++)
    tmp_b[i] = cp(b[i], 0);
  init(n);
  fft(tmp_a);
  fft(tmp_b);
  for (int i = 0; i < n; i++)
    tmp_a[i] *= tmp_b[i];
  ifft(tmp_a);
  s.resize(n);
  for (int i = 0; i < n; i++)
    s[i] = round(tmp_a[i].real());
  while ((int)s.size() > 1 && s.back() == 0)
    s.pop_back();
  return;
}
int main() {
  char c[maxn];
  bool flag = 0;
  scanf("%s", c);
  int n = strlen(c);
  if (c[0] == '-')
    flag ^= 1;
  for (int i = 0; i < n; i++) {
    if (c[i] != '-')
      a.push_back(c[i] - '0');
  }
  reverse(a.begin(), a.end());
  scanf("%s", c);
  if (c[0] == '-')
    flag ^= 1;
  n = strlen(c);
  for (int i = 0; i < n; i++) {
    if (c[i] != '-')
      b.push_back(c[i] - '0');
  }
  reverse(b.begin(), b.end());
  vector<int> ans;
  mul();
  n = (int)s.size();
  int cur = 0;
  for (int i = 0; i < n; i++) {
    cur = cur / 10 + s[i];
    ans.push_back(cur % 10);
  }
  cur /= 10;
  if (cur)
    ans.push_back(cur);
  reverse(ans.begin(), ans.end());
  if (!flag || ((int)ans.size() == 1 && ans[0] == 0)) {
    for (int i = 0; i < (int)ans.size(); i++)
      printf("%d", ans[i]);
    puts("");
  } else {
    putchar('-');
    for (int i = 0; i < (int)ans.size(); i++)
      printf("%d", ans[i]);
    puts("");
  }

  return 0;
}