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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
string w = "west";
string n = "north";
// #define int ll
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int m, int n) {
  if ((0 == m) || (0 == n))
    return 0;

  return ((m / gcd(m, n)) * n); // lcm = m * n / gcd(m,n)
}
signed main() {
  string s;
  while (cin >> s) {
    vector<string> data;
    if (s == "#")
      break;
    map<string, int> mp;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == 'w') {
        i += 3;
        data.push_back("west");
      } else {
        data.push_back("north");
        i += 4;
      }
    }
    reverse(all(data));
    ll now = 0;
    pii num = pii(0, 1);
    rep(i, data.size()) {
      if (!i) {
        if (data[i] == w) {
          now = 90;
          // mp[w]++;
        } else {
          now = 0;
          // mp[n]++;
        }
        continue;
      }
      if (data[i] == w) {
        mp[w]++;
        now = now * 2 + 90;
        cmax(now, 0);
      } else {
        mp[n]++;
        now = now * 2 - 90;
        cmax(now, 0);
      }
    }
    int let = gcd((int)now, pow(2, mp[n] + mp[w]));
    if ((int)pow(2, (mp[n] + mp[w])) / let == 1)
      cout << now / let << endl;
    else
      cout << (ll)now / let << "/" << (ll)pow(2, (mp[n] + mp[w])) / let << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define vi vector<int>
#define vvi vector<vector<int>>
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define all(in) in.begin(), in.end()
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
string w = "west";
string n = "north";
#define int ll
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int m, int n) {
  if ((0 == m) || (0 == n))
    return 0;

  return ((m / gcd(m, n)) * n); // lcm = m * n / gcd(m,n)
}
signed main() {
  string s;
  while (cin >> s) {
    vector<string> data;
    if (s == "#")
      break;
    map<string, int> mp;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == 'w') {
        // mp["west"]++;
        i += 3;
        data.push_back("west");
      } else {
        // mp["north"]++;
        data.push_back("north");
        i += 4;
      }
    }
    // rep(i,data.size())cout<<data[i]<<endl;
    reverse(all(data));
    int now = 0;
    pii num = pii(0, 1);
    rep(i, data.size()) {
      if (!i) {
        if (data[i] == w)
          num.first = 90;
        else
          num.first = 0;
        continue;
      }
      if (data[i] == w) {
        mp[w]++;
        now = num.first;
        num = pii(now * (int)pow(2, mp[w]) + 90 * num.second,
                  (int)pow(2, mp[w]) * num.second);
      } else {
        mp[n]++;
        now = num.first;
        num = pii(now * (int)pow(2, mp[n]) - 90 * num.second,
                  (int)pow(2, mp[n]) * num.second);
      }
    }
    int let = gcd(num.first, num.second);
    if (num.second / let <= 1)
      cout << num.first / let << endl;
    else
      cout << num.first / let << "/" << num.second / let << endl;
  }
}