Problem p04014 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#pragma region
using namespace std;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define rep(i, n) FOR(i, 0, n)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define LOWER(a, x) lower_bound(ALL(a), x) - a.begin() - 1;
#define COUNT(a, x) upper_bound(ALL(a), x) - lower_bound(ALL(a), x);
#define MCOUNT(a, x, y) upper_bound(ALL(a), y) - lower_bound(ALL(a), x);
#define scan(x) scanf("%lld", &x);
#define inf 5555555555555555555
#define mod 1000000007
#define pi 3.14159265358979323846
#define pb push_back
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef map<ll, ll> MAP;
typedef set<ll> SET;
ll n = 0, m = 0, ans = 0, sum = 0, cnt = 0, tmp = 0, ma = 0, mi = 0;
string s;
bool ok, flag;
ll dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
ll ddx[9] = {1, -1, 0, 0, 1, 1, -1, -1, 0},
   ddy[9] = {0, 0, 1, -1, 1, -1, 1, -1, 0};
#pragma endregion
#define MAX 111111

ll dsum(ll x, ll y) {
  ll ret = 0;
  while (x > 0) {
    ret += x % y;
    x /= y;
  }
  return ret;
}

int main(void) {
  cin >> n >> m;
  ans = inf;
  if (n < m) {
    cout << "-1" << endl;
    return 0;
  }
  if (n == m) {
    cout << n + 1 << endl;
    return 0;
  }
  FOR(i, 2, sqrt(n)) {
    if (dsum(n, i) == m)
      ans = min(ans, i);
  }
  FOR(i, 1, sqrt(n) + 1) {
    tmp = (n - m) / i + 1;
    if (dsum(n, tmp) == m)
      ans = min(ans, tmp);
  }

  if (ans == inf) {
    cout << "-1" << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#pragma region
using namespace std;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define rep(i, n) FOR(i, 0, n)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define LOWER(a, x) lower_bound(ALL(a), x) - a.begin() - 1;
#define COUNT(a, x) upper_bound(ALL(a), x) - lower_bound(ALL(a), x);
#define MCOUNT(a, x, y) upper_bound(ALL(a), y) - lower_bound(ALL(a), x);
#define scan(x) scanf("%lld", &x);
#define inf 5555555555555555555
#define mod 1000000007
#define pi 3.14159265358979323846
#define pb push_back
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef map<ll, ll> MAP;
typedef set<ll> SET;
ll n = 0, m = 0, ans = 0, sum = 0, cnt = 0, tmp = 0, ma = 0, mi = 0;
string s;
bool ok, flag;
ll dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
ll ddx[9] = {1, -1, 0, 0, 1, 1, -1, -1, 0},
   ddy[9] = {0, 0, 1, -1, 1, -1, 1, -1, 0};
#pragma endregion
#define MAX 111111

ll dsum(ll x, ll y) {
  ll ret = 0;
  if (y == 1)
    return x;
  while (x > 0) {
    ret += x % y;
    x /= y;
  }
  return ret;
}

int main(void) {
  cin >> n >> m;
  ans = inf;
  if (n < m) {
    cout << "-1" << endl;
    return 0;
  }
  if (n == m) {
    cout << n + 1 << endl;
    return 0;
  }
  FOR(i, 2, sqrt(n)) {
    if (dsum(n, i) == m)
      ans = min(ans, i);
  }
  FOR(i, 1, sqrt(n) + 1) {
    tmp = (n - m) / i + 1;
    if (dsum(n, tmp) == m)
      ans = min(ans, tmp);
  }

  if (ans == inf) {
    cout << "-1" << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}