Balanced Integer

시간 제한30초메모리 제한2048 MB

문제

Since the CCO often uses integers, Alice needs to learn about the integers! A positive integer $n$ can be written in base $b$ as the sequence $d_{m−1}d_{m−2} \dots d_1d_0$ if the following hold:

  • Each digit $d_i$ is between $0$ and $b − 1$, inclusive.
  • $d_{m−1} > 0$.
  • $n = d_{m−1} \times b^{m−1} + d_{m−2} \times b^{m−2} + \cdots + d_1 \times b^1 + d_0 \times b^0$.

For example, the integer $2025$ in base $19$ is the sequence $(5, 11, 11)$ because $2025 = 5 \times 19^2 + 11 \times 19^1 + 11 \times 19^0$.

An integer $n$ is $b$-balanced if, when $n$ is written in base $b$, the average of the digits is $\frac{b − 1}{ 2}$.

For example, $2025$ is $19$-balanced because $\frac{5 + 11 + 11}{ 3} = 9 = \frac{19 − 1}{ 2}$.

Alice can easily find integers that are $19$-balanced. However, she has trouble finding integers that are balanced in multiple ways. Given $B$ and $N$, please help Alice find the minimum integer $x$ such that:

  • $x$ is $b$-balanced, for all $2 ≤ b ≤ B$.
  • $x ≥ N$.

입력

The first line of input contains two space-separated integers $B$ and $N$ ($N ≥ 1$).

It is guaranteed that the answer does not exceed $10^{18}$.

출력

Output the minimum integer $x$ from the problem statement.

힌트

Feel free to use these code snippets as part of your solution.

// Important: If x is 0, the result is undefined.
int base_2_length(unsigned long long x) {
    return 64-__builtin_clzll(x);
}

int base_2_sum(unsigned long long x) {
    return __builtin_popcountll(x);
}