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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.IO;

#region Program
namespace Program
{
    #region Solver
    public class Solver
    {
        static IO.StreamScanner
        sc = new IO.StreamScanner(Console.OpenStandardInput());

        static readonly int noAnswer = -1;

        long n;
        long s;

        long b;

        long rn;

        #region Solver
        public Solver()
        {
            n = sc.Long();
            s = sc.Long();

            // n ^ (1 / 2)
            rn = (long)Math.Pow(n, 0.5);
        }
        #endregion

        #region Solve
        public void Solve()
        {
            // n < b ならば n と s が等しい
            if (n == s)
            {
                Console.WriteLine(n + 1);
                return;
            }

            // 条件より b は 2 以上
            // 2 <= b <= n ^ (1 / 2)
            // 全探索
            for (long i = 2; i <= rn; i++)
            {
                if (f(i, n) == s)
                {
                    Console.WriteLine(i);
                    return;
                }
            }

            // n ^ (1 / 2) < b
            // n < b ^ 2
            // b ^ 2 は n より大きく、定義に従い f(b, n) で分解される
            // n < b ならば n と s が等しい

            // n は b の 1次式 で表現することができる
            // n = p * b + q
            // s = p + q
            // (n - s) = p * (b - 1)

            // n - s の約数を求める
            // ただし n ^ (1 / 2) より大きいもの
            Console.WriteLine(divisor(n - s));
            return;
        }
        #endregion

        #region n
        long f(long i, long n)
        {
            // 割れなくなるまで続ける
            if (n / i != 0L) return f(i, n / i) + n % i;
            else return n % i;
        }
        #endregion

        #region divisor
        long divisor(long x)
        {
            // 1 を約数に含めない
            // O(n) ではなく、O(n ^ (1 / 2))まで計算量を落とす
            // 小さいほうから確認する
            for (long i = (long)Math.Pow(x, 0.5); 2L <= i; i--)
            {
                if ((x % i) == 0)
                {
                    // n ^ (1 / 2) より大きいもの
                    long xi = x / i;
                    if (rn < xi)
                    {
                        long xip = xi + 1;
                        // 題意を満たすか確認
                        if (f(xip, n) == s) return xip;
                    }
                }
            }

            long nsp = n - s + 1;

            // b が n と等しい場合、 s が 1 となる
            if (s == 1L) return n;
            else if (nsp == 0L) return noAnswer;
            // p = 1、すなわち n - s 自身が b - 1 の場合
            else if (f(nsp, n) == s) return nsp;
            else return noAnswer;
        }
        #endregion
    }
    #endregion
}
#endregion

#region Execute
class Execute
{
    public static void Main(string[] args)
    {
        new Program.Solver().Solve();
        Program.IO.Printer.Out.Flush();
    }
}
#endregion

#region Program.IO
namespace Program.IO
{
    using System.Text;
    using System.Globalization;
    using System.Linq;
    public class Printer : StreamWriter
    {
        static Printer()
        {
            Out = new Printer(Console.OpenStandardOutput())
            {
                AutoFlush = false
            };
        }
        public static Printer Out
        {
            get;
            set;
        }
        public override IFormatProvider FormatProvider
        {
            get
            {
                return CultureInfo.InvariantCulture;
            }
        }
        public Printer(System.IO.Stream stream)
            : base(stream, new UTF8Encoding(false, true))
        {
        }
        public Printer(System.IO.Stream stream, Encoding encoding)
            : base(stream, encoding)
        {
        }
        public void Write<T>(string format, T[] source)
        {
            base.Write(format, source.OfType<object>().ToArray());
        }
        public void WriteLine<T>(string format, T[] source)
        {
            base.WriteLine(format, source.OfType<object>().ToArray());
        }
    }
    public class StreamScanner
    {
        public readonly Stream str;
        private readonly byte[] buf = new byte[1024];
        private int len, ptr;
        public bool isEof = false;
        public StreamScanner(Stream stream)
        {
            str = stream;
        }
        private T[] enumerate<T>(int n, Func<T> f)
        {
            var a = new T[n];
            for (int i = 0; i < n; ++i) a[i] = f();
            return a;
        }
        public long Long()
        {
            if (isEof)
            {
                return long.MinValue;
            }
            long ret = 0;
            byte b = 0;
            var ng = false;
            do
            {
                b = read();
            }
            while (b != '-' && (b < '0' || '9' < b));
            if (b == '-')
            {
                ng = true;
                b = read();
            }
            for (; true; b = read())
            {
                if (b < '0' || '9' < b)
                {
                    return ng ? -ret : ret;
                }
                else ret = ret * 10 + b - '0';
            }
        }
        public long[] Long(int n)
        {
            return enumerate(n, Long);
        }
        public int Integer()
        {
            return (isEof) ? int.MinValue : (int)Long();
        }
        public int[] Integer(int n)
        {
            return enumerate(n, Integer);
        }
        public char[] Char(int n)
        {
            return enumerate(n, Char);
        }
        public char Char()
        {
            byte b = 0;
            do
            {
                b = read();
            }
            while ((b < 33 || 126 < b) && !isEof);
            return (char)b;
        }
        public string Scan()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
            {
                sb.Append(b);
            }
            return sb.ToString();
        }
        private byte read()
        {
            if (isEof)
            {
                return 0;
            }
            if (ptr >= len)
            {
                ptr = 0;
                if ((len = str.Read(buf, 0, 1024)) <= 0)
                {
                    isEof = true;
                    return 0;
                }
            }
            return buf[ptr++];
        }
    }
}
#endregion