Balanced Integer

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

요약
2부터 B까지 모든 진법 b에서 b진법 자릿수의 평균이 (b-1)/2가 되는, N 이상인 최소 정수 x를 구한다.
난이도

어려움10점 중 8점

유형
수학, 정수론, 이분 탐색, 그리디
정답자
아직 제출이 없습니다

문제

Since the CCO often uses integers, Alice needs to learn about the integers! A positive integer nn can be written in base bb as the sequence d_m−1d_m−2…d_1d_0d\_{m−1}d\_{m−2} \dots d\_1d\_0 if the following hold:

  • Each digit d_id\_i is between 00 and b−1b − 1, inclusive.
  • d_m−1>0d\_{m−1} > 0.
  • n=d_m−1×bm−1+d_m−2×bm−2+⋯+d_1×b1+d_0×b0n = 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 20252025 in base 1919 is the sequence (5,11,11)(5, 11, 11) because 2025=5×192+11×191+11×1902025 = 5 \times 19^2 + 11 \times 19^1 + 11 \times 19^0.

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

For example, 20252025 is 1919-balanced because 5+11+113=9=19−12\frac{5 + 11 + 11}{ 3} = 9 = \frac{19 − 1}{ 2}.

Alice can easily find integers that are 1919-balanced. However, she has trouble finding integers that are balanced in multiple ways. Given BB and NN, please help Alice find the minimum integer xx such that:

  • xx is bb-balanced, for all 2≤b≤B2 ≤ b ≤ B.
  • x≥Nx ≥ N.

입력

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

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

출력

Output the minimum integer xx 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);
}

예제2

  1. 예제 1

    입력
    4 100
    
    예상 출력
    141
    
  2. 예제 2

    입력
    7 10000000000
    
    예상 출력
    16926961207710