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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a, b) (a < b ? (a = b, 1) : 0)
#define chmin(a, b) (a > b ? (a = b, 1) : 0)
#define valid(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)
const int INF = 1 << 29;
const double EPS = 1e-8;
typedef long long ll;
typedef pair<int, int> pii;

typedef vector<double> vec;
typedef vector<vec> mat;

ostream &operator<<(ostream &os, const vec &a) {
  FOR(it, a) {
    double a = *it;
    if (abs(*it) < EPS * EPS)
      a = 0;
    os << setw(3) << a << " ";
  }
  return os;
}
ostream &operator<<(ostream &os, const mat &a) {
  FOR(it, a) os << *it << endl;
  return os;
}

bool GaussElimination(const mat &A, const vec &b, vec &res) {
  int n = A.size();
  mat B(n, vec(n + 1));
  REP(i, n) REP(j, n) B[i][j] = A[i][j];
  REP(i, n) B[i][n] = b[i];

  int nowy = 0;
  REP(x, n) {
    int pivot = -1;
    for (int j = nowy; j < n; ++j)
      if (pivot == -1 || abs(B[pivot][x]) < abs(B[j][x])) {
        pivot = j;
      }
    if (pivot == -1 || abs(B[pivot][x]) < EPS)
      continue;
    swap(B[nowy], B[pivot]);

    for (int j = nowy + 1; j < n; ++j) {
      double t = B[j][x] / B[nowy][x];
      for (int k = x; k <= n; ++k)
        B[j][k] = B[j][k] - B[nowy][k] * t;
    }
    nowy++;
  }
  res.clear();
  for (int y = nowy; y < n; ++y)
    if (abs(B[y][n]) > EPS) // rank(A) != rank(A|b)
      return 0;
  if (nowy != n) { // rank(A) == rank(A|b) != n
    res = vec(n, 1e100);
    for (int y = n - 1; y >= 0; --y) {
      int x;
      for (x = y; x < n; ++x) {
        if (abs(B[y][x]) > EPS)
          break;
      }
      if (x == n)
        continue;
      double sum = B[y][n];
      for (int i = x + 1; i < n; ++i) {
        if (res[i] == 1e100)
          res[i] = 0;
        sum -= res[i] * B[y][i];
      }
      res[x] = sum / B[y][x];
    }
    REP(i, n) if (res[i] == INF) res[i] = 0;
    return 0;
  }
  // ティツァツ」テ」ツ?古、ツクツ?ヲツ?湘」ツ?ォテヲツアツコテ」ツ?セテ」ツつ凝」ツ?づヲツ卍ョテゥツ?堙」ツ?ォテ・ツセツ古ゥツ??、ツサツ」テ・ツ?・
  res.resize(n);
  for (int x = n - 1; x >= 0; --x) {
    double sum = B[x][n];
    for (int i = n - 1; i > x; --i) {
      sum -= res[i] * B[x][i];
    }
    res[x] = sum / B[x][x];
  }
  return 1;
}

vector<string> split(string s, char c) {
  vector<string> ret;
  replace(ALL(s), c, ' ');
  stringstream ss(s);
  string t;
  while (ss >> t)
    ret.push_back(t);
  return ret;
}

int find(const string &s, char c) { return s.find(c); }

typedef map<string, int> MP;
char str[100];

MP group(int &id);
MP unit_group(int &id);
int number(int &id);

MP molecule(int &id) {
  MP res;
  while (isupper(str[id]) || str[id] == '(') {
    MP tmp = group(id);
    FOR(it, tmp) res[it->first] += it->second;
  }
  return res;
}

MP group(int &id) {
  MP res = unit_group(id);
  if (isdigit(str[id])) {
    int a = number(id);
    MP tmp;
    FOR(it, res) { tmp[it->first] = it->second * a; }
    res = tmp;
  }
  return res;
}
MP unit_group(int &id) {
  MP res;
  if (str[id] == '(') {
    id++;
    res = molecule(id);
    assert(str[id++] == ')');
  } else {
    assert(isupper(str[id]));
    int len = 1;
    if (islower(str[id + 1]))
      len = 2;
    string tmp(len, ' ');
    REP(i, len) tmp[i] = str[id + i];
    res[tmp] = 1;
    id += len;
  }
  return res;
}

int number(int &id) {
  int res = 0;
  while (isdigit(str[id])) {
    res *= 10;
    res += str[id] - '0';
    id++;
  }
  return res;
}

bool integer(double x) { return abs(x - round(x)) < EPS; }

int a[500][500];

int main() {
  string s;
  while (cin >> s, s != ".") {
    s = s.substr(0, s.size() - 1);
    int pos = find(s, '-');
    string s1 = s.substr(0, pos);
    string s2 = s.substr(pos + 2);
    vector<string> v1 = split(s1, '+');
    vector<string> v2 = split(s2, '+');
    memset(a, 0, sizeof(a));
    int kind = 0;
    map<string, int> mp;
    REP(i, v1.size()) {
      int id = 0;
      strcpy(str, v1[i].c_str());
      MP tmp = molecule(id);
      FOR(jt, tmp) {
        string s = jt->first;
        int num = jt->second;
        // cout << "A " << s << " " << num << endl;
        if (mp.count(s) == 0) {
          mp[s] = kind++;
        }
        a[mp[s]][i] = num;
      }
    }
    REP(i, v2.size()) {
      int id = 0;
      strcpy(str, v2[i].c_str());
      MP tmp = molecule(id);
      FOR(jt, tmp) {
        string s = jt->first;
        int num = jt->second;
        // cout << "B " << s << " " << num << endl;
        if (mp.count(s) == 0) {
          mp[s] = kind++;
        }
        a[mp[s]][v1.size() + i] = -num;
      }
    }
    a[kind++][0] = 1;
    int N = v1.size() + v2.size();
    mat A(kind, vec(kind));
    vec b(kind);
    REP(i, kind) REP(j, N) A[i][j] = a[i][j];
    // cout << A << endl; cout << endl;
    b[kind - 1] = 1;
    vec x;
    GaussElimination(A, b, x);
    // cout << "A" << endl;
    // cout << A.size() << " " << A[0].size() << endl;
    // cout << b.size() << endl;
    // cout << x << endl;
    for (int i = 1; i <= 40000; ++i) {
      vec t(x);
      bool ok = 1;
      FOR(it, t) if (!integer(*it *= i)) {
        ok = 0;
        break;
      }
      if (ok) {
        REP(i, N) {
          if (i)
            cout << " ";
          cout << t[i];
        }
        cout << endl;
        break;
      }
    }
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a, b) (a < b ? (a = b, 1) : 0)
#define chmin(a, b) (a > b ? (a = b, 1) : 0)
#define valid(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)
const int INF = 1 << 29;
const double EPS = 1e-8;
typedef long long ll;
typedef pair<int, int> pii;

typedef vector<double> vec;
typedef vector<vec> mat;

ostream &operator<<(ostream &os, const vec &a) {
  FOR(it, a) os << setw(6) << *it << " ";
  return os;
}
ostream &operator<<(ostream &os, const mat &a) {
  FOR(it, a) os << *it << endl;
  return os;
}

bool GaussElimination(const mat &A, const vec &b, vec &res) {
  int n = A.size();
  mat B(n, vec(n + 1));
  REP(i, n) REP(j, n) B[i][j] = A[i][j];
  REP(i, n) B[i][n] = b[i];

  int nowy = 0;
  REP(x, n) {
    int pivot = -1;
    for (int j = nowy; j < n; ++j)
      if (B[j][x]) {
        pivot = j;
        break;
      }
    if (pivot == -1)
      continue;
    swap(B[nowy], B[pivot]);

    for (int j = nowy + 1; j < n; ++j) {
      double t = B[j][x] / B[nowy][x];
      for (int k = x; k <= n; ++k)
        B[j][k] = B[j][k] - B[nowy][k] * t;
    }
    nowy++;
  }
  res.clear();
  for (int y = nowy; y < n; ++y)
    if (B[y][n]) // rank(A) != rank(A|b)
      return 0;
  if (nowy != n) { // rank(A) == rank(A|b) != n
    return 0;
  }
  // ティツァツ」テ」ツ?古、ツクツ?ヲツ?湘」ツ?ォテヲツアツコテ」ツ?セテ」ツつ凝」ツ?づヲツ卍ョテゥツ?堙」ツ?ォテ・ツセツ古ゥツ??、ツサツ」テ・ツ?・
  res.resize(n);
  for (int x = n - 1; x >= 0; --x) {
    double sum = B[x][n];
    for (int i = n - 1; i > x; --i) {
      sum -= res[i] * B[x][i];
    }
    res[x] = sum / B[x][x];
  }
  return 1;
}

vector<string> split(string s, char c) {
  vector<string> ret;
  replace(ALL(s), c, ' ');
  stringstream ss(s);
  string t;
  while (ss >> t)
    ret.push_back(t);
  return ret;
}

int find(const string &s, char c) { return s.find(c); }

typedef map<string, int> MP;
char str[100];

MP group(int &id);
MP unit_group(int &id);
int number(int &id);

MP molecule(int &id) {
  MP res;
  while (isupper(str[id]) || str[id] == '(') {
    MP tmp = group(id);
    FOR(it, tmp) res[it->first] += it->second;
  }
  return res;
}

MP group(int &id) {
  MP res = unit_group(id);
  if (isdigit(str[id])) {
    int a = number(id);
    MP tmp;
    FOR(it, res) { tmp[it->first] = it->second * a; }
    res = tmp;
  }
  return res;
}
MP unit_group(int &id) {
  MP res;
  if (str[id] == '(') {
    id++;
    res = molecule(id);
    assert(str[id++] == ')');
  } else {
    assert(isupper(str[id]));
    int len = 1;
    if (islower(str[id + 1]))
      len = 2;
    string tmp(len, ' ');
    REP(i, len) tmp[i] = str[id + i];
    res[tmp] = 1;
    id += len;
  }
  return res;
}

int number(int &id) {
  int res = 0;
  while (isdigit(str[id])) {
    res *= 10;
    res += str[id] - '0';
    id++;
  }
  return res;
}

bool integer(double x) { return abs(x - round(x)) < EPS; }

int a[500][500];

int main() {
  string s;
  while (cin >> s, s != ".") {
    s = s.substr(0, s.size() - 1);
    int pos = find(s, '-');
    string s1 = s.substr(0, pos);
    string s2 = s.substr(pos + 2);
    vector<string> v1 = split(s1, '+');
    vector<string> v2 = split(s2, '+');
    memset(a, 0, sizeof(a));
    int kind = 0;
    map<string, int> mp;
    REP(i, v1.size()) {
      int id = 0;
      strcpy(str, v1[i].c_str());
      MP tmp = molecule(id);
      FOR(jt, tmp) {
        string s = jt->first;
        int num = jt->second;
        // cout << "A " << s << " " << num << endl;
        if (mp.count(s) == 0) {
          mp[s] = kind++;
        }
        a[mp[s]][i] = num;
      }
    }
    REP(i, v2.size()) {
      int id = 0;
      strcpy(str, v2[i].c_str());
      MP tmp = molecule(id);
      FOR(jt, tmp) {
        string s = jt->first;
        int num = jt->second;
        // cout << "B " << s << " " << num << endl;
        if (mp.count(s) == 0) {
          mp[s] = kind++;
        }
        a[mp[s]][v1.size() + i] = -num;
      }
    }
    a[kind++][0] = 1;
    int N = v1.size() + v2.size();
    mat A(kind, vec(N));
    vec b(kind);
    REP(i, kind) REP(j, N) A[i][j] = a[i][j];
    b[kind - 1] = 1;
    // cout << A << endl;
    // cout << b << endl;
    vec x;
    GaussElimination(A, b, x);
    for (int i = 1; i <= 40000; ++i) {
      vec t(x);
      bool ok = 1;
      FOR(it, t) if (!integer(*it *= i)) {
        ok = 0;
        break;
      }
      if (ok) {
        REP(i, N) {
          if (i)
            cout << " ";
          cout << t[i];
        }
        cout << endl;
        break;
      }
    }
  }
}