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: Accepted)

use std::io::BufRead;

fn main() {
    let stdin = std::io::stdin();
    let n = stdin.lock().gets().parse::<u64>().unwrap();
    let s = stdin.lock().gets().parse::<u64>().unwrap();

    if n < s {
        println!("-1");
        return;
    } else if n == s {
        println!("{}", n + 1);
        return;
    }
    let mut divisors = list_divisors(n - s);
    divisors.sort();
    for b in divisors.into_iter().map(|x| x + 1) {
        let mut n2 = n;
        let mut s2 = 0;
        while n2 > 0 {
            s2 += n2 % b;
            n2 /= b;
        }
        if s == s2 {
            println!("{}", b);
            return;
        }
    }
    println!("-1");
}

fn list_divisors(mut n: u64) -> Vec<u64> {
    let mut divisors = vec![1];
    let mut sqrt = ((n - 1) as f64).sqrt() as u64 + 1;
    let sieve_len = (sqrt - 1) as usize / 2;
    let mut sieve = vec![0u64; (sieve_len.saturating_sub(1)) / 64 + 1];
    let mut f = 2;
    let mut si = 0;
    while n > 1 {
        let (mut beg, len) = (0, divisors.len());
        while n % f == 0 {
            n /= f;
            sqrt = (n as f64).sqrt() as u64;
            for i in beg..beg + len {
                let t = divisors[i];
                divisors.push(t * f);
            }
            beg += len;
        }
        while si < sieve_len && (sieve[si / 64] & 1 << si % 64) != 0 {
            si += 1;
        }
        f = si as u64 * 2 + 3;
        if f > sqrt {
            f = n;
            continue;
        }
        let mut i = si;
        while i < sieve_len {
            sieve[i / 64] |= 1 << i % 64;
            i += f as usize;
        }
    }
    divisors
}

trait Gets { fn gets(&mut self) -> String; }
impl<T: std::io::BufRead> Gets for T {
    fn gets(&mut self) -> String { self.lines().next().unwrap().unwrap() } }