Problem p03282 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PI 3.14159265359
#define MOD 1000000007
typedef long long LL;

template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> void print_vector(vector<T> vec) {
  cout << "[ ";
  for (int i = 0; i < vec.size(); i++) {
    cout << vec[i] << " ";
  }
  cout << "]" << endl;
}
template <class T> void print_pair_vector(vector<pair<T, T>> vec) {
  cout << "[ ";
  for (int i = 0; i < vec.size(); i++) {
    cout << "(" << vec[i].first << "," << vec[i].second << ") ";
  }
  cout << "]" << endl;
}

const int INF = 1 << 30;

int main(void) {
  string S;
  cin >> S;

  LL K;
  cin >> K;

  LL elapsed_days = 5000000000000000;
  LL total_length = 0;
  for (LL i = 0; i < S.size(); i++) {
    LL num = LL(S[i] - '0');
    if (num == 1) {
      total_length += 1;
    } else {
      LL tmp = 1;
      for (LL j = 0; j < elapsed_days; j++) {
        tmp *= num;
        LL next_total_length = total_length + tmp / num;
        if (next_total_length < total_length) {
          total_length = 1LL << 60;
        } else {
          total_length = next_total_length;
        }

        if (K <= total_length) {
          cout << num << endl;
          return 0;
        }
      }
    }
  }
  return 1;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PI 3.14159265359
#define MOD 1000000007
typedef long long LL;

template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> void print_vector(vector<T> vec) {
  cout << "[ ";
  for (int i = 0; i < vec.size(); i++) {
    cout << vec[i] << " ";
  }
  cout << "]" << endl;
}
template <class T> void print_pair_vector(vector<pair<T, T>> vec) {
  cout << "[ ";
  for (int i = 0; i < vec.size(); i++) {
    cout << "(" << vec[i].first << "," << vec[i].second << ") ";
  }
  cout << "]" << endl;
}

const int INF = 1 << 30;

int main(void) {
  string S;
  cin >> S;

  LL K;
  cin >> K;

  LL elapsed_days = 5000000000000000;
  LL total_length = 0;
  for (LL i = 0; i < S.size(); i++) {
    LL num = LL(S[i] - '0');
    if (num == 1) {
      total_length += 1;
      if (K <= total_length) {
        cout << num << endl;
        return 0;
      }
    } else {
      LL tmp = 1;
      for (LL j = 0; j < elapsed_days; j++) {
        tmp *= num;
        LL next_total_length = total_length + tmp / num;
        if (next_total_length < total_length) {
          total_length = 1LL << 60;
        } else {
          total_length = next_total_length;
        }

        if (K <= total_length) {
          cout << num << endl;
          return 0;
        }
      }
    }
  }
  return 1;
}