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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()
const int INF = 1e9;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PII;

/* struct P {
        int x, y, n;
        P(int n, int x, int y):n(n), x(x), y(y){}
        P(){}
}; */

/** Problem2589 : North North West **/
string dir;

ll _gcd(ll a, ll b) {
  while (a) {
    b %= a;
    swap(a, b);
  }
  return b;
}

PII reduction(PII a) {
  ll gcd = _gcd(a.first, a.second);
  a.first /= gcd;
  a.second /= gcd;
  if (a.second < 0) {
    a.first *= -1;
    a.second *= -1;
  }
  return a;
}

PII operator-(PII a) { return PII(-a.first, a.second); }

ll lcm(ll a, ll b) { return a * b / _gcd(a, b); }

PII add(PII a, PII b) {
  PII res;

  res.second = lcm(a.second, b.second);
  res.first = a.first * res.second / a.second + b.first * res.second / b.second;

  return reduction(res);
}

PII solve(int pos) {
  if (pos == dir.size() - 1)
    return PII((dir[pos] == 'n' ? 0 : 90), 1);

  int n = (int)dir.size() - pos - 1;

  PII res, a = solve(pos + 1), b = PII(90, pow(2.0, n));

  return (dir[pos] == 'w' ? add(a, b) : add(a, -b));
}

int main() {
  string S;

  while (cin >> S, S != "#") {
    dir = "";

    for (int i = 0; i < S.size(); i++) {
      if (S[i] == 'w' || S[i] == 'n')
        dir += S[i];
    }

    PII ans = reduction(solve(0));

    if (ans.first == 0 || ans.second == 1) {
      cout << ans.first << endl;
    } else {
      cout << ans.first << "/" << ans.second << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()
const int INF = 1e9;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;

/* struct P {
        int x, y, n;
        P(int n, int x, int y):n(n), x(x), y(y){}
        P(){}
}; */

/** Problem2589 : North North West **/
string dir;

PII reduction(PII a) {
  int gcd = __gcd(a.first, a.second);
  a.first /= gcd;
  a.second /= gcd;
  if (a.second < 0) {
    a.first *= -1;
    a.second *= -1;
  }
  return a;
}

PII operator-(PII a) { return PII(-a.first, a.second); }

int lcm(int a, int b) { return a * b / __gcd(a, b); }

PII add(PII a, PII b) {
  PII res;

  res.second = lcm(a.second, b.second);
  res.first = a.first * res.second / a.second + b.first * res.second / b.second;

  return reduction(res);
}

PII solve(int pos) {
  if (pos == dir.size() - 1)
    return PII((dir[pos] == 'n' ? 0 : 90), 1);

  int n = (int)dir.size() - pos - 1;

  PII res, a = solve(pos + 1), b = PII(90, pow(2.0, n));

  return (dir[pos] == 'w' ? add(a, b) : add(a, -b));
}

int main() {
  string S;

  while (cin >> S, S != "#") {
    dir = "";

    for (int i = 0; i < S.size(); i++) {
      if (S[i] == 'w' || S[i] == 'n')
        dir += S[i];
    }

    PII ans = reduction(solve(0));

    if (ans.first == 0 || ans.second == 1) {
      cout << ans.first << endl;
    } else {
      cout << ans.first << "/" << ans.second << endl;
    }
  }
}