Problem:
Score : 500 points

Problem Statement

For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:

f(b,n) = n, when n < b

f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b

Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b,
and n \ {\rm mod} \ b denotes the remainder of n divided by b.

Less formally, f(b,n) is equal to the sum of the digits of n written in base b.
For example, the following hold:

f(10,\,87654)=8+7+6+5+4=30

f(100,\,87654)=8+76+54=138

You are given integers n and s.
Determine if there exists an integer b (b \geq 2) such that f(b,n)=s.
If the answer is positive, also find the smallest such b.

Constraints

1 \leq n \leq 10^{11}

1 \leq s \leq 10^{11}

n,\,s are integers.

Input

The input is given from Standard Input in the following format:

n
s

Output

If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b.
If such b does not exist, print -1 instead.

Sample Input 1

87654
30

Sample Output 1

10

Sample Input 2

87654
138

Sample Output 2

100

Sample Input 3

87654
45678

Sample Output 3

-1

Sample Input 4

31415926535
1

Sample Output 4

31415926535

Sample Input 5

1
31415926535

Sample Output 5

-1

Solution in C++11 (GCC 4.8.1) (judge verdict: Accepted)

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <iomanip>
#include <limits>
#include <list>
#include <queue>
#include <tuple>
#include <map>
#include <stack>
#include <set>
using namespace std;
#define MOD (long long int)(1e9+7)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define INF (int)(1123456789)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
#define all(v) v.begin(), v.end()

const int N = 1234567;

ll mpow(ll a, ll b){
	if(b==0) return 1;
	else if(b%2==0){ll memo = mpow(a,b/2); return memo*memo%MOD;}
	else return mpow(a,b-1) * a % MOD;
}
ll gcd(ll a, ll b){
	if(b==0) return a;
	else return gcd(b, a%b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n){
	if(kaijo_memo.size() > n) return kaijo_memo[n];
	if(kaijo_memo.size() == 0) kaijo_memo.push_back(1);
	while(kaijo_memo.size() <= n) kaijo_memo.push_back(kaijo_memo[kaijo_memo.size()-1] * kaijo_memo.size() % MOD);
	return kaijo_memo[n];
}
ll nCr(ll n, ll r){
	if(n < r || r < 0) return 0;
	ll ret = 1;
	ret *= kaijo(n); ret %= MOD;
	ret *= mpow(kaijo(r), MOD-2); ret %= MOD;
	ret *= mpow(kaijo(n-r), MOD-2); ret %= MOD;
	return ret;
}

ll calcS(ll n, ll b){
	return n/b  + n%b;
}

int main(void){
	ll n_memo,s;
	cin>>n_memo>>s;
	ll n = n_memo;
	if(n < s){
		cout<<-1<<endl;
		return 0;
	}
	if(n==s){
		cout<<s+1<<endl;
		return 0;
	}

	reps(b, (int)1e6){
		if(b<=1) continue;
		ll wa = 0;
		n = n_memo;
		while(n > 0){
			wa += n%b;
			n /= b;
		}
		if(wa == s){
			cout<<b<<endl;
			return 0;
		}
	}
	n = n_memo;
	if((int)1e6 > n){
		cout<<-1<<endl;
		return 0;
	}

	vector<ll> yakusu;
	reps(i, sqrt(n-s)+1){
		if((n-s)%i == 0){
			yakusu.push_back(i);
			yakusu.push_back((n-s)/i);
		}
	}
	ll ans = LINF;
	rep(i, yakusu.size()){
		ll b = yakusu[i] + 1;
		if(b <= (int)1e6) continue;
		if(n/b + n%b == s){
			ans = min(ans, b);
		}
	}
	if(ans != LINF){
		cout<<ans<<endl;
		return 0;
	}
	cout<<-1<<endl;
	/*
	ll begin = (ll)1e6 - 2;
	ll end = n+1;
	while(end - begin > 1){
		ll mid = (end + begin) / 2;
		if(calcS(n, mid) < s){
			begin = mid;
		}else{
			end = mid;
		}
	}
	if(calcS(n, begin) == s){
		cout<<begin<<endl;
	}else if(calcS(n,end) == s){
		cout<<end<<endl;
	}else{
		cout<<-1<<endl;
	}*/
	return 0;
}