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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
typedef long long ll;
typedef vector<ll> poly;
ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
poly operator+(poly x, poly y) {
  poly ret(10, 0);
  rep(i, 10) ret[i] = x[i] + y[i];
  return ret;
}
poly operator-(poly x, poly y) {
  poly ret(10, 0);
  rep(i, 10) ret[i] = x[i] - y[i];
  return ret;
}
poly operator*(poly x, poly y) {
  poly ret(10, 0);
  rep(i, 10) rep(j, 10 - i) ret[i + j] += x[i] * y[j];
  return ret;
}
int deg(poly x) {
  for (int i = 9; i >= 0; i--)
    if (x[i] != 0)
      return i;
  return -1;
}
void showpoly(poly x) {
  int g = 0;
  rep(i, 10) g = gcd(g, x[i]);
  if (g)
    rep(i, 10) x[i] /= g;
  int N = deg(x);
  if (x[N] < 0)
    x = poly(10, 0) - x;
  for (int i = N; i >= 0; i--) {
    if (x[i] == 0)
      continue;
    if (i != N && x[i] > 0)
      cout << "+";
    if (x[i] == -1)
      cout << "-";
    else if (x[i] != 1)
      cout << x[i];
    if (i == 0) {
      if (x[i] == 1 || x[i] == -1)
        cout << "1";
    } else if (i == 1) {
      cout << "x";
    } else {
      cout << "x^" << i;
    }
  }
  puts("");
}
string s;
int num(int &it) {
  int x = 0;
  while (isdigit(s[it])) {
    x = x * 10 + s[it] - '0';
    it++;
  }
  return x;
}
poly X(10, 0);
poly cons(int x) {
  poly a(10, 0);
  a[0] = x;
  return a;
}
poly ex(poly x, int p) {
  poly ret = cons(1);
  rep(i, p) ret = ret * x;
  return ret;
}
poly expr(int &it);
poly primary(int &it) {
  if (s[it] == '(') {
    it++;
    poly ret = expr(it);
    it++;
    return ret;
  }
  if (isdigit(s[it]))
    return cons(num(it));
  if (s[it] == 'x') {
    it++;
    return X;
  }
  assert(false);
}
poly factor(int &it) {
  poly ret = primary(it);
  if (s[it] == '^') {
    it++;
    int x = num(it);
    ret = ex(ret, x);
  }
  return ret;
}
poly term(int &it) {
  poly ret = factor(it);
  while (s[it] != ')' && s[it] != '+' && s[it] != '-') {
    poly x = factor(it);
    ret = ret * x;
  }
  return ret;
}
poly expr(int &it) {
  poly ret;
  if (s[it] == '-')
    ret = cons(0) - term(++it);
  else
    ret = term(it);
  while (s[it] == '+' || s[it] == '-') {
    char c = s[it++];
    poly x = term(it);
    if (c == '+')
      ret = ret + x;
    else
      ret = ret - x;
  }
  return ret;
}
int main() {
  X[1] = 1;
  while (true) {
    cin >> s;
    if (s[0] == '.')
      break;
    s += ")";
    int it = 0;
    poly x = expr(it);
    cin >> s;
    s += ")";
    it = 0;
    poly y = expr(it);
    if (deg(x) < deg(y))
      swap(x, y);
    while (deg(y) >= 0) {
      int N = deg(x), M = deg(y);
      rep(i, N - M + 1) {
        ll a = x[N - i], b = y[M];
        ll c = b / gcd(a, b);
        rep(j, 10) x[j] *= c;
        c = x[N - i] / b;
        rep(j, M + 1) x[N - i - j] -= c * y[M - j];
        ll g = 0;
        rep(j, 10) g = gcd(g, x[j]);
        if (g)
          rep(j, 10) x[j] /= g;
      }
      swap(x, y);
    }
    showpoly(x);
  }
}

Error: -6
d6dc4414-edf1-4bd1-b3c8-8bce7eeeeaba.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00872/C++/s171015312.cpp:91: poly primary(int&): Assertion `false' failed.


Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
typedef long long ll;
typedef vector<ll> poly;
ll gcd(ll x, ll y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
poly operator+(poly x, poly y) {
  poly ret(10, 0);
  rep(i, 10) ret[i] = x[i] + y[i];
  return ret;
}
poly operator-(poly x, poly y) {
  poly ret(10, 0);
  rep(i, 10) ret[i] = x[i] - y[i];
  return ret;
}
poly operator*(poly x, poly y) {
  poly ret(10, 0);
  rep(i, 10) rep(j, 10 - i) ret[i + j] += x[i] * y[j];
  return ret;
}
int deg(poly x) {
  for (int i = 9; i >= 0; i--)
    if (x[i] != 0)
      return i;
  return -1;
}
void showpoly(poly x) {
  int g = 0;
  rep(i, 10) g = gcd(g, x[i]);
  rep(i, 10) x[i] /= g;
  int N = deg(x);
  if (x[N] < 0)
    x = poly(10, 0) - x;
  for (int i = N; i >= 0; i--) {
    if (x[i] == 0)
      continue;
    if (i != N && x[i] > 0)
      cout << "+";
    if (x[i] == -1)
      cout << "-";
    else if (x[i] != 1)
      cout << x[i];
    if (i == 0) {
      if (x[i] == 1 || x[i] == -1)
        cout << "1";
    } else if (i == 1) {
      cout << "x";
    } else {
      cout << "x^" << i;
    }
  }
  puts("");
}
string s;
int num(int &it) {
  int x = 0;
  while (isdigit(s[it])) {
    x = x * 10 + s[it] - '0';
    it++;
  }
  return x;
}
poly X(10, 0);
poly cons(int x) {
  poly a(10, 0);
  a[0] = x;
  return a;
}
poly ex(poly x, int p) {
  poly ret = cons(1);
  rep(i, p) ret = ret * x;
  return ret;
}
poly expr(int &it);
poly primary(int &it) {
  if (s[it] == '(') {
    it++;
    poly ret = expr(it);
    it++;
    return ret;
  }
  if (isdigit(s[it]))
    return cons(num(it));
  if (s[it] == 'x') {
    it++;
    return X;
  }
  assert(false);
}
poly factor(int &it) {
  poly ret = primary(it);
  if (s[it] == '^') {
    it++;
    int x = num(it);
    ret = ex(ret, x);
  }
  return ret;
}
poly term(int &it) {
  poly ret = factor(it);
  while (s[it] != ')' && s[it] != '+' && s[it] != '-') {
    poly x = factor(it);
    ret = ret * x;
  }
  return ret;
}
poly expr(int &it) {
  poly ret;
  if (s[it] == '-')
    ret = cons(0) - term(++it);
  else
    ret = term(it);
  while (s[it] == '+' || s[it] == '-') {
    char c = s[it++];
    poly x = term(it);
    if (c == '+')
      ret = ret + x;
    else
      ret = ret - x;
  }
  return ret;
}
int main() {
  X[1] = 1;
  while (true) {
    cin >> s;
    s += ")";
    int it = 0;
    poly x = expr(it);
    //		showpoly(x);
    cin >> s;
    s += ")";
    it = 0;
    poly y = expr(it);
    if (deg(x) < deg(y))
      swap(x, y);
    while (deg(y) >= 0) {
      int N = deg(x), M = deg(y);
      rep(i, N - M + 1) {
        ll a = x[N - i], b = y[M];
        ll c = b / gcd(a, b);
        rep(j, 10) x[j] *= c;
        c = x[N - i] / b;
        rep(j, M + 1) x[N - i - j] -= c * y[M - j];
        int g = 0;
        rep(j, M + 1) g = gcd(g, x[N - i - j]);
        if (g)
          rep(j, M + 1) x[N - i - j] /= g;
      }
      swap(x, y);
    }
    showpoly(x);
  }
}