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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))
#define iter(v, ite) for (auto ite = (v).begin(); ite != (v).end(); ite++)
#define mimunum(v, x)                                                          \\
  distance((v).begin(), lower_bound((v).begin(), (v).end(), x))

typedef pair<char, lli> cip;

typedef string::iterator sta;

bool isnum(char c) { return ('0' <= c && c <= '9'); }

lli num(sta &s) {
  lli res = 0;
  while (isnum(*s)) {
    res *= 10;
    res += (*s - '0');
    s++;
  }
  return res;
}

cip exprs(sta &s, lli p);

#include <cassert>

// 6(55(44(255(YG))A)7(97(R))W9(U)PJ)G9(347(Y84(QSKVZ)JJ262(I779(UDOQ)RXSQV3(VTCY))W))4(B)NDJ
// 65420

cip expr(sta &s, lli p) {
  // printf("%c %lld\\n",*s,p);
  cip res;
  if (isnum(*s)) {
    lli a = num(s);
    if (*s == '(') {
      s++;
      sta ns = s;
      cip pa = exprs(s, p);
      if (pa.fir != '0')
        res = pa;
      else {
        if (pa.sec * a > p) {
          res = exprs(ns, p % pa.sec);
        } else
          res = cip('0', pa.sec * a);
      }
      s++;
    } else {
      if (a > p)
        res = cip(*s, 0);
      else
        res = cip('0', a);
      s++;
    }
  } else {
    if (p == 0)
      res = cip(*s, 0);
    else
      res = cip('0', 1);
    s++;
  }
  return res;
}

cip exprs(sta &s, lli p) {
  cip res('0', 0);
  lli nl = 0;
  for (;;) {
    if (*s == ')' || *s == '$')
      break;
    cip a = expr(s, p);
    // printf("expr %c %d\\n",a.fir,a.sec);
    if (a.fir != '0')
      return a;
    p -= a.sec;
    nl += a.sec;
  }
  res.sec = nl;
  return res;
}

int n;
string s;

int main(void) {
  for (;;) {
    cin >> s >> n;
    if (n == 0 && s[0] == '0' && s[1] == '\\0')
      break;
    s += "$";
    sta ss = s.begin();
    cip pa = exprs(ss, n);
    printf("%c\\n", pa.fir);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++)
#define irep(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pque(type) priority_queue<type, vector<type>, greater<type>>
#define memst(a, b) memset(a, b, sizeof(a))
#define iter(v, ite) for (auto ite = (v).begin(); ite != (v).end(); ite++)
#define mimunum(v, x)                                                          \\
  distance((v).begin(), lower_bound((v).begin(), (v).end(), x))

typedef pair<char, lli> cip;

typedef string::iterator sta;

bool isnum(char c) { return ('0' <= c && c <= '9'); }

lli num(sta &s) {
  lli res = 0;
  while (isnum(*s)) {
    res *= 10;
    res += (*s - '0');
    s++;
  }
  return res;
}

cip exprs(sta &s, lli p);

#include <cassert>

cip expr(sta &s, lli p) {
  // printf("%c %lld\\n",*s,p);
  cip res;
  if (isnum(*s)) {
    lli a = num(s);
    if (*s == '(') {
      s++;
      sta ns = s;
      cip pa = exprs(s, p);
      if (pa.fir != '0')
        res = pa;
      else {
        if (pa.sec * a > p) {
          res = exprs(ns, p % pa.sec);
        } else
          res = cip('0', pa.sec * a);
      }
      assert(*s == ')');
      s++;
    } else {
      if (a > p)
        res = cip(*s, 0);
      else
        res = cip('0', a);
      s++;
    }
  } else {
    if (p == 0)
      res = cip(*s, 0);
    else
      res = cip('0', 1);
    s++;
  }
  return res;
}

cip exprs(sta &s, lli p) {
  cip res('0', 0);
  lli nl = 0;
  for (;;) {
    if (*s == ')' || *s == '$')
      break;
    cip a = expr(s, p);
    // printf("expr %c %d\\n",a.fir,a.sec);
    if (a.fir != '0')
      return a;
    p -= a.sec;
    nl += a.sec;
  }
  res.sec = nl;
  return res;
}

int n;
string s;

int main(void) {
  for (;;) {
    cin >> s >> n;
    if (n == 0 && s[0] == '0' && s[1] == '\\0')
      break;
    s += "$";
    sta ss = s.begin();
    cip pa = exprs(ss, n);
    printf("%c\\n", pa.fir);
  }
  return 0;
}