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 Rust (1.15.1) (judge verdict: Wrong Answer)

use std::str::FromStr;
use std::io::*;
use utils::*;

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(i.lock(), &mut o);
    stdout().write_all(&o).unwrap();
}

fn run<R: BufRead, W: Write>(i: R, o: &mut W) {
    let mut i = AtRead::from(i);
    let n = i.read::<usize>();
    let s = i.read::<usize>();

    let result = if let Some(r) = solve(n, s) {
        r.to_string()
    } else {
        "-1".to_string()
    };
    writeln!(o, "{}", result).unwrap();
}
fn solve(n: usize, s: usize) -> Option<usize> {
    fn f(b: usize, n: usize) -> usize {
        if n < b {
            n
        } else {
            n % b + f(b, n / b)
        }
    }
    let mut result = None;
    if n == s {
        result = Some(n + 1);
    }
    if n < s {
        return None;
    }

    let mut b_last = n + 1;
    for s1 in 1..s + 1 {
        let s0 = s - s1;
        let n1 = n - s0;
        let b = n1 / s1;
        if s1 >= b {
            break;
        }
        b_last = b;
        if n1 % s1 == 0 && s0 < b {
            result = Some(b);
        }
    }
    if s == 1 {
        return result;
    }

    for b in 2..b_last {
        if f(b, n) == s {
            return Some(b);
        }
    }
    result
}

mod utils {
    use super::*;

    pub struct AtRead<R: BufRead> {
        r: R,
        s: String,
    }

    impl<R: BufRead> AtRead<R> {
        pub fn from(r: R) -> Self {
            AtRead {
                r: r,
                s: String::new(),
            }
        }
        pub fn read_line(&mut self) -> &str {
            self.s.clear();
            self.r.read_line(&mut self.s).unwrap();
            self.s.trim()
        }

        pub fn read<T: FromStr>(&mut self) -> T {
            self.read_line().parse().ok().unwrap()
        }
    }
}