Problem p03913 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
const long double eps = 1e-12;
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define GYAKU 500000004LL
#define MOD 998244353LL
#define seg_size 262144 * 2
#define REP(a, b) for (long long a = 0; a < b; ++a)
long long solve(long long based, long long kazu, long long bader,
                long long border) {
  long long now_cookie = 1;
  REP(i, bader) {
    if (now_cookie > border / (based - 1LL)) {
      return true;
    }
    now_cookie *= based - 1LL;
  }
  REP(i, kazu - bader) {
    if (now_cookie > border / (based)) {
      return true;
    }
    now_cookie *= based;
  }
  if (now_cookie >= border)
    return true;
  return false;
}
int main() {
  long long n, a;
  cin >> n >> a;
  long long ans = n;
  for (long long use_cookie = 1; use_cookie < 40; ++use_cookie) {
    long long bot = 1e18;
    long long top = 0;
    while (abs(top - bot) > 1) {
      long long mid = (top + bot) / 2;
      if (solve(mid, use_cookie + 1, 0, n) == true) {
        bot = mid;
      } else
        top = mid;
    }
    for (long long q = 0; q <= use_cookie; ++q) {
      if (solve(bot, use_cookie + 1, q, n) == 0)
        break;
      ans = min(ans, bot * (use_cookie + 1LL) + use_cookie * a - q);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
const long double eps = 1e-12;
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define GYAKU 500000004LL
#define MOD 998244353LL
#define seg_size 262144 * 2
#define REP(a, b) for (long long a = 0; a < b; ++a)
long long solve(long long based, long long kazu, long long bader,
                long long border) {
  long long now_cookie = 1;
  REP(i, bader) {
    if (based != 1)
      if (now_cookie > border / (based - 1LL)) {
        return true;
      }
    now_cookie *= based - 1LL;
  }
  REP(i, kazu - bader) {
    if (now_cookie > border / (based)) {
      return true;
    }
    now_cookie *= based;
  }
  if (now_cookie >= border)
    return true;
  return false;
}
int main() {
  long long n, a;
  cin >> n >> a;
  long long ans = n;
  for (long long use_cookie = 1; use_cookie < 40; ++use_cookie) {
    long long bot = 1e18;
    long long top = 0;
    while (abs(top - bot) > 1) {
      long long mid = (top + bot) / 2;
      if (solve(mid, use_cookie + 1, 0, n) == true) {
        bot = mid;
      } else
        top = mid;
    }
    for (long long q = 0; q <= use_cookie; ++q) {
      if (solve(bot, use_cookie + 1, q, n) == 0)
        break;
      ans = min(ans, bot * (use_cookie + 1LL) + use_cookie * a - q);
    }
  }
  cout << ans << endl;
}