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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <complex> // complex<double> a(1.2 , 2.3);// real(): 1.2, imag()2.3
using namespace std;

#define MOD 1000000007
#define ll long long
#define ld long double
#define FOR(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rmsame(a) sort(all(a)),a.erase(unique(all(a)), a.end())
#define rmvector(a,b) rep(i,a.size())rep(j,b.size())if(a[i]==b[j]){a.erase(a.begin()+i);i--;break;}
template<typename X> bool exist(vector<X> vec, X item){return find(all(vec), item)!=vec.end();}

// n<b, f(b,n) = n;
// n>=b, f(b,n) = f(b, n/b) + (n mod b)
// inp n, s;
// f(b,n) = s, b=?
// b進で表したらpqで、すべての桁の和がsになる
// 10進で表したら、 nになる

int f(ll b,ll n){
  if(n < b ) return n;
  else return (n%b) + f(b,n/b);
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, b, s;
  cin >> n >> s; 

  
  rep(i, sqrt(n)){
    if(i == 0 || i==1) continue;
    b =  i;
    if(f(b, n) == s){
      cout << b << endl;
      return 0;
    }
  }

  vector<ll> posq1;
  // q p;
  // q + p = s;
  // q * b + p = n
  // b = (n-s) / (q-1)
  // よって、bは(n-s) の約数

  // q==0の可能性もあるのでそれを先に例外処理する。
  // その場合p(s) = n , b > p
  if(s == n) {
    cout << s + 1 << endl;
    return 0;
  }

  
  ll ans = n+n;
  // q == 1の可能性もある
  // その場合 p = s-1
  // b = (n-p) / q = (n-s+1)
  // その場合、b > pが必須
  if(((n-s+1) > (s-1))&& ((s-1) >= 0)){
    ans = n-s+1;
  }
  
  
  rep(i, sqrt(n-s)+1){
    if(i == 0 || i == 1) continue;
    if((n-s) % i == 0){
      posq1.pb(i);
      posq1.pb((n-s)/i);
    }
  }
  


  rep(i, posq1.size()){
    b = (n-s) / posq1[i];
    if(b < 2) continue;
    if(f(b, n) == s)
      ans = min(ans, b);
  }

  if(ans != (n+n)) cout << ans << endl;
  else cout << "-1" << endl;


  //cout << fixed << setprecision(16) << ans << endl;
  return 0;
}