Problem p04016 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
// #define  NDEBUG

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define all(v) v.begin(), v.end()
#define db(x) cout << #x << " = " << (x) << "\n"
#define fend(x) ((x) & ((x) + 1)) - 1
#define fenu(x) (x) | ((x) + 1)
#define forn(i, n) for (int i = 0; i < (int)n; ++i)
#define for1n(i, b, e) for (int i = b; i < (int)e; ++i)
#define ft first
#define len(s) s.length()
#define mp std::make_pair
#define pob pop_back
#define pof pop_front
#define pub push_back
#define puf push_front
#define sc second

typedef double dbl;
typedef long double ldbl;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;

const long long MILLER_RABIN = 3215031751;
const long double PI = acos(-1);

// struct config_io { config_io() { cin.tie(nullptr);
// ios_base::sync_with_stdio(false); } } cnf_io; struct config_rand {
// config_rand() {
// srand(chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count());
// } } cnf_rand;

// namespace std
//{
//	template<>
//	struct hash < pair<int, int> > {
//		size_t operator()(const pair<int, int> &x) const {
//			return (x.first * 31 + x.second) % ((int)1e9 + 7);
//		}
//	};
// }

// __builtin_popcount(x) - Returns the number of 1-bits in x.
// __builtin_parity(x) - Returns the parity of x, i.e. the number of 1-bits in x
// modulo 2.
// __builtin_ffs(x) - Returns one plus the index of the least significant 1-bit
// of x, or if x is zero, returns zero.
// __builtin_clz(x) - Returns the number of leading 0-bits in x, starting at the
// most significant bit position. If x is 0, the result is undefined.
// __builtin_ctz(x) - Returns the number of trailing 0-bits in x, starting at
// the least significant bit position. If x is 0, the result is undefined.

inline void scan(int &x) { scanf("%d", &x); }
inline void scan(long long &x) { scanf("%lld", &x); }
inline void scan(double &x) { scanf("%lf", &x); }
inline void scan(long double &x) { scanf("%Lf", &x); }
inline void scan(int &x, int &y) { scanf("%d%d", &x, &y); }
inline void scan(long long &x, long long &y) { scanf("%lld%lld", &x, &y); }
inline void scan(int &x, int &y, int &z) { scanf("%d%d%d", &x, &y, &z); }
inline void scan(int &w, int &x, int &y, int &z) {
  scanf("%d%d%d%d", &w, &x, &y, &z);
}
inline void print(const int x) { printf("%d\n", x); }
inline void print(const long long x) { printf("%lld\n", x); }
inline void print(const double x) { printf("%lf\n", x); }
inline void print(const long double x) { printf("%Lf\n", x); }
inline void print(const int x, const int y) { printf("%d %d\n", x, y); }
inline void print(const int x, const int y, const int z) {
  printf("%d %d %d\n", x, y, z);
}

inline void tobin(long long x) { cout << bitset<63>(x) << endl; }
template <class T> inline void trace(T collection) {
  for (auto elem : collection) {
    cout << elem << endl;
  }
}

// const int ML = 1e5 + 10;
// int t[4 * ML];
// vector<int> v[ML];
//
// void ass(int v, int tl, int tr, int pos, int value) {
//
// }
//
// int get(int v, int tl, int tr, int l, int r) {
//
// }
//
// void solve() {
//	int n;
//	scan(n);
//	vector<int> x(n), y(n);
//	vector<int> xx(n), yy(n);
//	forn(i, n) {
//		scan(x[i], y[i]);
//		xx[i] = x[i], yy[i] = y[i];
//	}
//
//	sort(all(xx)), sort(all(yy));
//	int xx_size = unique(all(xx)) - xx.begin();
//	xx.resize(xx_size);
//	int yy_size = unique(all(yy)) - yy.begin();
//	yy.resize(yy_size);
//	map<int, int> xmap, ymap;
//	forn(i, xx.size()) {
//		xmap[xx[i]] = i;
//	}
//	forn(i, yy.size()) {
//		ymap[yy[i]] = i;
//	}
//
//	forn(i, n) {
//		v[ymap[y[i]]].push_back(xmap[x[i]]);
//	}
//	forn(i, yy_size) {
//		if (v[i].size()) {
//			sort(all(v[i]));
//		}
//	}
//
//	int xsize = xmap.size();
//	forn(y, ML) {
//		vector<int> mem(v[y].size());
//		forn(j, v[y].size()) {
//			int cur = get(1, 0, xsize, 0, v[y][j] - 1);
//			mem[j] = cur + 1;
//		}
//
//		forn(j, v[y].size()) {
//			if (get(1, 0, xsize, v[y][j], v[y][j]) < mem[j]) {
//				ass(1, 0, xsize, v[y][j], mem[j]);
//			}
//		}
//	}
// }

ll n, s;

bool check_this_b(ll b) {
  ll sum = 0;
  ll nn = n;
  while (nn > 0) {
    sum += nn % b;
    nn /= b;
  }
  return sum == s;
}

void solve(int test) {
  scan(n, s);
  ll diff = n - s;
  if (diff < 0) {
    print(-1);
    return;
  }
  ll ans = 1e18;
  for (ll i = 1; i * i <= diff; ++i) {
    if (diff % i == 0) {
      if (check_this_b(i + 1)) {
        ans = min(ans, i + 1);
      }
      if (check_this_b(diff / i + 1)) {
        ans = min(ans, diff / i + 1);
      }
    }
  }
  if (check_this_b(diff + 1)) {
    ans = min(ans, diff + 1);
  }
  print(ans == 1e18 ? -1 : ans);
}

int main() {
#ifdef HOME
#define HOME
  freopen("home.in", "r", stdin);
  freopen("home.out", "w", stdout);
#endif

  int tests = 1;
  // scan(tests);
  for (int test = 1; test <= tests; ++test) {
    solve(test);
  }

#ifdef HOME
  cout << "\n\nTime: " << clock() / (double)CLOCKS_PER_SEC << endl;
#endif
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
// #define  NDEBUG

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define all(v) v.begin(), v.end()
#define db(x) cout << #x << " = " << (x) << "\n"
#define fend(x) ((x) & ((x) + 1)) - 1
#define fenu(x) (x) | ((x) + 1)
#define forn(i, n) for (int i = 0; i < (int)n; ++i)
#define for1n(i, b, e) for (int i = b; i < (int)e; ++i)
#define ft first
#define len(s) s.length()
#define mp std::make_pair
#define pob pop_back
#define pof pop_front
#define pub push_back
#define puf push_front
#define sc second

typedef double dbl;
typedef long double ldbl;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;

const long long MILLER_RABIN = 3215031751;
const long double PI = acos(-1);

// struct config_io { config_io() { cin.tie(nullptr);
// ios_base::sync_with_stdio(false); } } cnf_io; struct config_rand {
// config_rand() {
// srand(chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count());
// } } cnf_rand;

// namespace std
//{
//	template<>
//	struct hash < pair<int, int> > {
//		size_t operator()(const pair<int, int> &x) const {
//			return (x.first * 31 + x.second) % ((int)1e9 + 7);
//		}
//	};
// }

// __builtin_popcount(x) - Returns the number of 1-bits in x.
// __builtin_parity(x) - Returns the parity of x, i.e. the number of 1-bits in x
// modulo 2.
// __builtin_ffs(x) - Returns one plus the index of the least significant 1-bit
// of x, or if x is zero, returns zero.
// __builtin_clz(x) - Returns the number of leading 0-bits in x, starting at the
// most significant bit position. If x is 0, the result is undefined.
// __builtin_ctz(x) - Returns the number of trailing 0-bits in x, starting at
// the least significant bit position. If x is 0, the result is undefined.

inline void scan(int &x) { scanf("%d", &x); }
inline void scan(long long &x) { scanf("%lld", &x); }
inline void scan(double &x) { scanf("%lf", &x); }
inline void scan(long double &x) { scanf("%Lf", &x); }
inline void scan(int &x, int &y) { scanf("%d%d", &x, &y); }
inline void scan(long long &x, long long &y) { scanf("%lld%lld", &x, &y); }
inline void scan(int &x, int &y, int &z) { scanf("%d%d%d", &x, &y, &z); }
inline void scan(int &w, int &x, int &y, int &z) {
  scanf("%d%d%d%d", &w, &x, &y, &z);
}
inline void print(const int x) { printf("%d\n", x); }
inline void print(const long long x) { printf("%lld\n", x); }
inline void print(const double x) { printf("%lf\n", x); }
inline void print(const long double x) { printf("%Lf\n", x); }
inline void print(const int x, const int y) { printf("%d %d\n", x, y); }
inline void print(const int x, const int y, const int z) {
  printf("%d %d %d\n", x, y, z);
}

inline void tobin(long long x) { cout << bitset<63>(x) << endl; }
template <class T> inline void trace(T collection) {
  for (auto elem : collection) {
    cout << elem << endl;
  }
}

// const int ML = 1e5 + 10;
// int t[4 * ML];
// vector<int> v[ML];
//
// void ass(int v, int tl, int tr, int pos, int value) {
//
// }
//
// int get(int v, int tl, int tr, int l, int r) {
//
// }
//
// void solve() {
//	int n;
//	scan(n);
//	vector<int> x(n), y(n);
//	vector<int> xx(n), yy(n);
//	forn(i, n) {
//		scan(x[i], y[i]);
//		xx[i] = x[i], yy[i] = y[i];
//	}
//
//	sort(all(xx)), sort(all(yy));
//	int xx_size = unique(all(xx)) - xx.begin();
//	xx.resize(xx_size);
//	int yy_size = unique(all(yy)) - yy.begin();
//	yy.resize(yy_size);
//	map<int, int> xmap, ymap;
//	forn(i, xx.size()) {
//		xmap[xx[i]] = i;
//	}
//	forn(i, yy.size()) {
//		ymap[yy[i]] = i;
//	}
//
//	forn(i, n) {
//		v[ymap[y[i]]].push_back(xmap[x[i]]);
//	}
//	forn(i, yy_size) {
//		if (v[i].size()) {
//			sort(all(v[i]));
//		}
//	}
//
//	int xsize = xmap.size();
//	forn(y, ML) {
//		vector<int> mem(v[y].size());
//		forn(j, v[y].size()) {
//			int cur = get(1, 0, xsize, 0, v[y][j] - 1);
//			mem[j] = cur + 1;
//		}
//
//		forn(j, v[y].size()) {
//			if (get(1, 0, xsize, v[y][j], v[y][j]) < mem[j]) {
//				ass(1, 0, xsize, v[y][j], mem[j]);
//			}
//		}
//	}
// }

ll n, s;

bool check_this_b(ll b) {
  ll sum = 0;
  ll nn = n;
  while (nn > 0) {
    sum += nn % b;
    nn /= b;
  }
  return sum == s;
}

void solve(int test) {
  scan(n, s);
  ll diff = n - s;
  if (diff < 0) {
    print(-1);
    return;
  }
  if (diff == 0) {
    print(n + 1);
    return;
  }
  ll ans = 1e18;
  for (ll i = 1; i * i <= diff; ++i) {
    if (diff % i == 0) {
      if (check_this_b(i + 1)) {
        ans = min(ans, i + 1);
      }
      if (check_this_b(diff / i + 1)) {
        ans = min(ans, diff / i + 1);
      }
    }
  }
  if (check_this_b(diff + 1)) {
    ans = min(ans, diff + 1);
  }
  print(ans == 1e18 ? -1 : ans);
}

int main() {
#ifdef HOME
#define HOME
  freopen("home.in", "r", stdin);
  freopen("home.out", "w", stdout);
#endif

  int tests = 1;
  // scan(tests);
  for (int test = 1; test <= tests; ++test) {
    solve(test);
  }

#ifdef HOME
  cout << "\n\nTime: " << clock() / (double)CLOCKS_PER_SEC << endl;
#endif
  return 0;
}