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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using CompLib.Util;

public class Program
{
    private long N, S;

    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextLong();
        S = sc.NextLong();

        // NをB進表記 桁和がSになるB
        // 1桁
        if (N == S)
        {
            Console.WriteLine(N + 1);
            return;
        }

        // 3桁以上
        long b = 2;
        for (; b * b <= N; b++)
        {
            if (F(b, N) == S)
            {
                Console.WriteLine(b);
                return;
            }
        }

        // 2桁
        // aB + c = N
        // a + c = S
        // a(B-1) = N-S
        long ans = long.MaxValue;
        {
            long T = N - S;
            for (long a = 1; a * a <= T; a++)
            {
                if (T % a == 0)
                {
                    long c = T / a;
                    if(F(a+1,N) == S) ans = Math.Min(ans, a+1);
                    if (F(c + 1, N) == S) ans = Math.Min(ans, c + 1);
                }
            }
        }
        if (ans == long.MaxValue)
        {
            Console.WriteLine("-1");
            return;
        }
        Console.WriteLine(ans);
    }

    long F(long b, long n)
    {
        if (n < b) return n;
        return F(b, n / b) + (n % b);
    }

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            if (_index >= _line.Length)
            {
                string s;
                do
                {
                    s = Console.ReadLine();
                } while (s.Length == 0);

                _line = s.Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public string ReadLine()
        {
            _index = _line.Length;
            return Console.ReadLine();
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            string s = Console.ReadLine();
            _line = s.Length == 0 ? new string[0] : s.Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}