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


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 1000000000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;

ll gcd(ll a, ll b) {
  if (a == 0 or b == 0) {
    return 1;
  }
  ll aa = max(a, b);
  ll bb = min(a, b);
  while (aa % bb != 0) {
    ll tmp = bb;
    bb = aa % bb;
    aa = tmp;
  }
  return bb;
}

pair<ll, ll> g(ll aa, ll ab, ll ba, ll bb) {
  ll c = ab * bb;
  ll d = aa * bb - ba * ab;
  ll cd = gcd(d, c);
  return {d / cd, c / cd};
}

pair<ll, ll> h(ll aa, ll ab, ll ba, ll bb) {
  ll c = ab * bb;
  ll d = aa * bb + ba * ab;
  ll cd = gcd(d, c);
  return {d / cd, c / cd};
}

ll fac2(int n) {
  if (n == 0) {
    return 1;
  } else {
    return 2 * fac2(n - 1);
  }
}

int main() {
  for (;;) {
    string s;
    cin >> s;
    if (s == "#")
      break;
    vector<int> dir;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == 'n') {
        dir.push_back(1);
        i += 4;
      } else {
        dir.push_back(2);
        i += 3;
      }
    }
    pair<ll, ll> ans = {0, 0};
    if (dir[dir.size() - 1] == 1) {
      ans = {0, 1};
    } else {
      ans = {90, 1};
    }
    for (int i = (int)dir.size() - 2; i >= 0; i--) {
      if (dir[i] == 1) {
        ans = g(ans.first, ans.second, 90, fac2((int)dir.size() - 1 - i));
      } else {
        ans = h(ans.first, ans.second, 90, fac2((int)dir.size() - 1 - i));
      }
    }
    if (ans.second == 1) {
      cout << ans.first << endl;
    } else {
      cout << ans.first << "/" << ans.second << endl;
    }
  }
}

Error: 0

Fixed submission:


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 1000000000
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;

int gcd(int a, int b) {
  int aa = max(a, b);
  int bb = min(a, b);
  while (aa % bb != 0) {
    int tmp = bb;
    bb = aa % bb;
    aa = tmp;
  }
  return bb;
}

pair<int, int> g(int aa, int ab, int ba, int bb) {
  int c = ab * bb;
  int d = aa * bb - ba * ab;
  if (d == 0)
    return {0, 1};
  int cd = gcd(d, c);
  return {d / cd, c / cd};
}
pair<int, int> h(int aa, int ab, int ba, int bb) {
  int c = ab * bb;
  int d = aa * bb + ba * ab;
  if (d == 0)
    return {0, 1};
  int cd = gcd(d, c);
  return {d / cd, c / cd};
}

int fac2(int n) {
  if (n == 0) {
    return 1;
  } else {
    return 2 * fac2(n - 1);
  }
}

int main() {
  for (;;) {
    string s;
    cin >> s;
    if (s == "#")
      break;
    vector<int> dir;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == 'n') {
        dir.push_back(1);
        i += 4;
      } else {
        dir.push_back(2);
        i += 3;
      }
    }
    pair<int, int> ans = {0, 0};
    if (dir[dir.size() - 1] == 1) {
      ans = {0, 1};
    } else {
      ans = {90, 1};
    }
    for (int i = (int)dir.size() - 2; i >= 0; i--) {
      if (dir[i] == 1) {
        ans = g(ans.first, ans.second, 90, fac2((int)dir.size() - 1 - i));
      } else {
        ans = h(ans.first, ans.second, 90, fac2((int)dir.size() - 1 - i));
      }
    }
    if (ans.second == 1) {
      cout << ans.first << endl;
    } else {
      cout << ans.first << "/" << ans.second << endl;
    }
  }
}