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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

class Fraq {
public:
  ll denominator;
  ll numerator;
  Fraq() {
    denominator = 0;
    numerator = 1;
  }
  Fraq(ll d, ll n) {
    denominator = d;
    numerator = n;
  }
  Fraq reduction() {
    Fraq res;
    ll x = max(denominator, numerator), y = min(denominator, numerator);
    while (x != 0 && y != 0) {
      x %= y;
      swap(x, y);
    }
    x = max(x, y);
    res.denominator = denominator / x;
    res.numerator = numerator / x;
    return res;
  }

  Fraq operator+(const Fraq &f) const {
    Fraq res(denominator * f.numerator + f.denominator * numerator,
             numerator * f.numerator);
    return res.reduction();
  }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  string str;
  while (cin >> str) {
    if (str == "#")
      return 0;
    ll north = 0, west = 0;
    vector<bool> b; // true:north false:west
    for (ll i = 0; i < str.length(); i++) {
      if (str[i] == 'n') {
        b.push_back(true);
        north++;
        i += 4;
      } else if (str[i] == 'w') {
        b.push_back(false);
        west++;
        i += 3;
      } else {
        assert(false);
      }
    }
    if (west == 0) {
      cout << 0 << endl;
      continue;
    } else if (north == 0) {
      cout << 90 << endl;
      continue;
    }

    reverse(b.begin(), b.end());
    Fraq ans(((b[0] == false) ? 90 : 0), 1);

    for (ll i = 1; i < b.size(); i++) {
      if (b[i] == false) {
        ans = ans + Fraq(90, pow(2, i));
      } else {
        ans = ans + Fraq(-90, pow(2, i));
      }
    }

    ans = ans.reduction();
    if (ans.numerator == 1)
      cout << ans.denominator << endl;
    else
      cout << ans.denominator << '/' << ans.numerator << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

class Fraq {
public:
  int denominator;
  int numerator;
  Fraq() {
    denominator = 0;
    numerator = 1;
  }
  Fraq(int d, int n) {
    denominator = d;
    numerator = n;
  }
  Fraq reduction() {
    Fraq res;
    int x = max(denominator, numerator), y = min(denominator, numerator);
    while (x != 0 && y != 0) {
      x %= y;
      swap(x, y);
    }
    x = max(x, y);
    res.denominator = denominator / x;
    res.numerator = numerator / x;
    return res;
  }

  Fraq operator+(const Fraq &f) const {
    Fraq res(denominator * f.numerator + f.denominator * numerator,
             numerator * f.numerator);
    return res.reduction();
  }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  string str;
  while (cin >> str) {
    if (str == "#")
      return 0;
    int north = 0, west = 0;
    vector<bool> b; // true:north false:west
    for (int i = 0; i < str.length(); i++) {
      if (str[i] == 'n') {
        b.push_back(true);
        north++;
        i += 4;
      } else if (str[i] == 'w') {
        b.push_back(false);
        west++;
        i += 3;
      } else {
        assert(false);
      }
    }
    if (west == 0) {
      cout << 0 << endl;
      continue;
    } else if (north == 0) {
      cout << 90 << endl;
      continue;
    }

    reverse(b.begin(), b.end());
    Fraq ans(((b[0] == false) ? 90 : 0), 1);

    for (int i = 1; i < b.size(); i++) {
      if (b[i] == false) {
        ans = ans + Fraq(90, pow(2, i));
      } else {
        ans = ans + Fraq(-90, pow(2, i));
      }
    }

    ans = ans.reduction();
    if (ans.numerator == 1)
      cout << ans.denominator << endl;
    else
      cout << ans.denominator << '/' << ans.numerator << endl;
  }
}