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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

#define int long long
typedef map<string, int> T;
typedef vector<T> F;
T operator*(int a, map<string, int> T) {
  for (auto it = T.begin(); it != T.end(); ++it)
    it->second *= a;
  return T;
}

string s;
int p;

void view(T ri) {
  for (auto it = ri.begin(); it != ri.end(); ++it) {
    cout << it->first << " " << it->second << endl;
  }
  cout << endl;
}

F m_s();
T mor();
T gr();
T u_g();
string c_e();
int numberz();

F m_s() {
  F le;
  le.push_back(mor());
  while (s[p] == '+') {
    p++;
    le.push_back(mor());
  }
  return le;
}
T mor() {
  T le = gr();
  while (s[p] != '.' && s[p] != '+' && s[p] != ')') {
    T ri = gr();
    for (auto it = ri.begin(); it != ri.end(); ++it) {
      le[it->first] += it->second;
    }
  }
  return le;
}
T gr() {
  T le = u_g();
  if (s[p] >= '1' && s[p] <= '9') {
    int num = numberz();
    le = num * le;
  }
  return le;
}
T u_g() {
  T le;
  if (s[p] == '(') {
    p++;
    le = mor();
    p++;
  } else {
    string hoge = c_e();
    le[hoge] = 1;
  }
  return le;
}
string c_e() {
  string v;
  v += s[p++];
  while (s[p] >= 'a' && s[p] <= 'z')
    v += s[p++];
  return v;
}
int numberz() {
  int v = 0;
  while (s[p] >= '0' && s[p] <= '9') {
    v = v * 10 + s[p++] - '0';
  }
  return v;
}

F parse(string x) {
  s = x;
  p = 0;
  return m_s();
}

#include <sstream>

#define lcm(a, b) ((a) / __gcd((a), (b)) * (b))
// a / b
class number {
public:
  long long a, b;
  number() { a = 0, b = 1; }
  number(long long X) { a = X, b = 1; }
  number(long long X, long long Y) {
    a = X, b = Y;
    fix();
  }
  void fix() {
    if (a == 0)
      b = 1;
    if (b < 0)
      a *= -1, b *= -1;
    int minu = a < 0 ? -1 : 1;
    if (a < 0)
      a = -a;
    long long g = __gcd(a, b);
    a /= g;
    b /= g;
    a *= minu;
  }
  string show() {
    stringstream ss;
    if (b == 1) {
      ss << a;
    } else {
      ss << a << "/" << b;
    }
    return ss.str();
  }
};
number operator+(const number &f1, const number &f2) {
  long long dem = lcm(f1.b, f2.b);
  long long A = f1.a * dem / f1.b;
  long long B = f2.a * dem / f2.b;
  number ret;
  ret.a = A + B;
  ret.b = dem;
  ret.fix();
  return ret;
}
number operator-(const number &f1, const number &f2) {
  long long dem = lcm(f1.b, f2.b);
  long long A = dem / f1.b * f1.a;
  long long B = dem / f2.b * f2.a;
  number ret;
  ret.a = A - B;
  ret.b = dem;
  ret.fix();

  return ret;
}
number operator*(const number &f1, const number &f2) {
  long long A = f1.a * f2.a;
  long long B = f1.b * f2.b;
  number ret;
  ret.a = A;
  ret.b = B;
  ret.fix();
  return ret;
}
number operator/(const number &f1, const number &f2) {
  long long A = f1.a * f2.b;
  long long B = f1.b * f2.a;
  number ret;
  ret.a = A;
  ret.b = B;
  ret.fix();
  return ret;
}
bool operator==(const number &f1, const number &f2) {
  return f1.a == f2.a && f1.b == f2.b;
}
typedef vector<number> Array;
typedef vector<Array> Matrix;

void view(Matrix A) {
  for (int i = 0; i < A.size(); i++) {
    for (int j = 0; j < A[0].size(); j++) {
      cout << A[i][j].show() << " ";
    }
    cout << endl;
  }
}
vector<long long> calc(Matrix A) {
  for (int i = 0; i < A.size(); i++) {
    for (int j = i; j < A.size(); j++) {
      if (!(A[j][i] == 0)) {
        for (int k = 0; k < A[0].size(); k++) {
          swap(A[i][k], A[j][k]);
        }
        break;
      }
    }
    number v = A[i][i];
    if (v.a == 0)
      continue;
    for (int j = 0; j < A[0].size(); j++) {
      A[i][j] = A[i][j] / v;
    }
    for (int j = 0; j < A.size(); j++) {
      if (i != j) {
        number coef = A[j][i];
        for (int k = 0; k < A[0].size(); k++) {
          A[j][k] = A[j][k] - coef * A[i][k];
        }
      }
    }
  }
  long long lc = 1;
  for (int i = 0; i < A.size(); i++) {
    if (A[i].back().a == 0)
      continue;
    lc = lcm(lc, A[i].back().b);
  }
  vector<long long> v;
  for (int i = 0; i < A.size(); i++) {
    if (A[i].back().a == 0)
      continue;
    v.push_back(lc / A[i].back().b * A[i].back().a);
  }
  return v;
}

#undef int
int main() {
#define int long long
  string str;
  while (cin >> str && str != ".") {
    int x = str.find("->");
    string a = str.substr(0, x) + ".";
    string b = str.substr(x + 2);
    F X = parse(a);
    F Y = parse(b);
    set<string> appear;
    for (int i = 0; i < X.size(); i++) {
      // view(X[i]);
      for (auto it = X[i].begin(); it != X[i].end(); ++it) {
        appear.insert(it->first);
      }
    }
    for (int i = 0; i < Y.size(); i++) {
      // view(Y[i]);
      for (auto it = Y[i].begin(); it != Y[i].end(); ++it) {
        appear.insert(it->first);
      }
    }
    int N = X.size() + Y.size();
    vector<vector<number>> mat;
    vector<number> init;
    for (int i = 0; i < N; i++)
      init.push_back(i == 0);
    init.push_back(1);
    mat.push_back(init);
    init.pop_back();

    for (auto it = appear.begin(); it != appear.end(); ++it) {
      vector<number> row;
      for (int i = 0; i < X.size(); i++)
        row.push_back(X[i][*it]);
      for (int i = 0; i < Y.size(); i++)
        row.push_back(-Y[i][*it]);
      row.push_back(0);
      mat.push_back(row);
    }
    vector<long long> res = calc(mat);
    for (int i = 0; i < res.size(); i++)
      cout << res[i] << (res.size() == i + 1 ? "\\n" : " ");
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

#define int long long
typedef map<string, int> T;
typedef vector<T> F;
T operator*(int a, map<string, int> T) {
  for (auto it = T.begin(); it != T.end(); ++it)
    it->second *= a;
  return T;
}

string s;
int p;

void view(T ri) {
  for (auto it = ri.begin(); it != ri.end(); ++it) {
    cout << it->first << " " << it->second << endl;
  }
  cout << endl;
}

F m_s();
T mor();
T gr();
T u_g();
string c_e();
int numberz();

F m_s() {
  F le;
  le.push_back(mor());
  while (s[p] == '+') {
    p++;
    le.push_back(mor());
  }
  return le;
}
T mor() {
  T le = gr();
  while (s[p] != '.' && s[p] != '+' && s[p] != ')') {
    T ri = gr();
    for (auto it = ri.begin(); it != ri.end(); ++it) {
      le[it->first] += it->second;
    }
  }
  return le;
}
T gr() {
  T le = u_g();
  if (s[p] >= '1' && s[p] <= '9') {
    int num = numberz();
    le = num * le;
  }
  return le;
}
T u_g() {
  T le;
  if (s[p] == '(') {
    p++;
    le = mor();
    p++;
  } else {
    string hoge = c_e();
    le[hoge] = 1;
  }
  return le;
}
string c_e() {
  string v;
  v += s[p++];
  while (s[p] >= 'a' && s[p] <= 'z')
    v += s[p++];
  return v;
}
int numberz() {
  int v = 0;
  while (s[p] >= '0' && s[p] <= '9') {
    v = v * 10 + s[p++] - '0';
  }
  return v;
}

F parse(string x) {
  s = x;
  p = 0;
  return m_s();
}

#include <sstream>

#define lcm(a, b) ((a) / __gcd((a), (b)) * (b))
// a / b
class number {
public:
  long long a, b;
  number() { a = 0, b = 1; }
  number(long long X) { a = X, b = 1; }
  number(long long X, long long Y) {
    a = X, b = Y;
    fix();
  }
  void fix() {
    if (a == 0)
      b = 1;
    if (b < 0)
      a *= -1, b *= -1;
    int minu = a < 0 ? -1 : 1;
    if (a < 0)
      a = -a;
    long long g = __gcd(a, b);
    a /= g;
    b /= g;
    a *= minu;
  }
  string show() {
    stringstream ss;
    if (b == 1) {
      ss << a;
    } else {
      ss << a << "/" << b;
    }
    return ss.str();
  }
};
number operator+(const number &f1, const number &f2) {
  long long dem = lcm(f1.b, f2.b);
  long long A = f1.a * dem / f1.b;
  long long B = f2.a * dem / f2.b;
  number ret;
  ret.a = A + B;
  ret.b = dem;
  ret.fix();
  return ret;
}
number operator-(const number &f1, const number &f2) {
  long long dem = lcm(f1.b, f2.b);
  long long A = dem / f1.b * f1.a;
  long long B = dem / f2.b * f2.a;
  number ret;
  ret.a = A - B;
  ret.b = dem;
  ret.fix();

  return ret;
}
number operator*(const number &f1, const number &f2) {
  long long A = f1.a * f2.a;
  long long B = f1.b * f2.b;
  number ret;
  ret.a = A;
  ret.b = B;
  ret.fix();
  return ret;
}
number operator/(const number &f1, const number &f2) {
  long long A = f1.a * f2.b;
  long long B = f1.b * f2.a;
  number ret;
  ret.a = A;
  ret.b = B;
  ret.fix();
  return ret;
}
bool operator==(const number &f1, const number &f2) { return (f1 - f2).a == 0; }
typedef vector<number> Array;
typedef vector<Array> Matrix;

void view(Matrix A) {
  for (int i = 0; i < A.size(); i++) {
    for (int j = 0; j < A[0].size(); j++) {
      cout << A[i][j].show() << " ";
    }
    cout << endl;
  }
}
vector<long long> calc(Matrix A) {
  for (int i = 0; i < A.size(); i++) {
    for (int j = i; j < A.size(); j++) {
      if (!(A[j][i] == 0)) {
        for (int k = 0; k < A[0].size(); k++) {
          swap(A[i][k], A[j][k]);
        }
        break;
      }
    }
    number v = A[i][i];
    for (int j = 0; j < A[0].size(); j++) {
      A[i][j] = A[i][j] / v;
    }
    for (int j = 0; j < A.size(); j++) {
      if (i != j) {
        number coef = A[j][i];
        for (int k = 0; k < A[0].size(); k++) {
          A[j][k] = A[j][k] - coef * A[i][k];
        }
      }
    }
  }
  long long lc = 1;
  for (int i = 0; i < A.size(); i++) {
    if (A[i].back().a == 0)
      continue;
    lc = lcm(lc, A[i].back().b);
  }
  vector<long long> v;
  for (int i = 0; i < A.size(); i++) {
    if (A[i].back().a == 0)
      continue;
    v.push_back(lc / A[i].back().b * A[i].back().a);
  }
  return v;
}

#undef int
int main() {
#define int long long
  string str;
  while (cin >> str && str != ".") {
    int x = str.find("->");
    string a = str.substr(0, x) + ".";
    string b = str.substr(x + 2);
    F X = parse(a);
    F Y = parse(b);
    set<string> appear;
    for (int i = 0; i < X.size(); i++) {
      // view(X[i]);
      for (auto it = X[i].begin(); it != X[i].end(); ++it) {
        appear.insert(it->first);
      }
    }
    for (int i = 0; i < Y.size(); i++) {
      // view(Y[i]);
      for (auto it = Y[i].begin(); it != Y[i].end(); ++it) {
        appear.insert(it->first);
      }
    }
    int N = X.size() + Y.size();
    vector<vector<number>> mat;
    vector<number> init;
    for (int i = 0; i < N; i++)
      init.push_back(i == 0);
    init.push_back(1);
    mat.push_back(init);
    init.pop_back();

    for (auto it = appear.begin(); it != appear.end(); ++it) {
      vector<number> row;
      for (int i = 0; i < X.size(); i++)
        row.push_back(X[i][*it]);
      for (int i = 0; i < Y.size(); i++)
        row.push_back(-Y[i][*it]);
      row.push_back(0);
      mat.push_back(row);
    }
    vector<long long> res = calc(mat);
    for (int i = 0; i < res.size(); i++)
      cout << res[i] << (res.size() == i + 1 ? "\\n" : " ");
  }
}