Search for numbers in different databases

I recently resolved a problem when I came across this: APAC Round E Q2

Basically the question asks to find the smallest base (> 1) in which, if a number (input) is written, then the number will consist of only 1s. Like 3, if the one presented in base 2 becomes 1 (consisting of only 1 s).

Now I tried to solve this problem with brute force, checking all bases from 2 to number to find such a base. But restrictions required more effective.

Can anyone help on how to approach this?

+4
source share
2 answers

O( (log2 n)^2 ), , , , 2 :

1 + r + r^2 + r^3 ... + r^(n-1) = (1 - r^n) / (1 - r)

Renaming the variables, we get: 

  n = (1 - base^power) / (1 - base)

power (floor(log2 n) + 1) 2, . :

n = 13:

  p = floor(log2 13) + 1 = 4:

    Binary search for base:

    (1 - 13^4) / (1 - 13) = 2380

    ...

    No match for power = 4. 

    Try power = 3:

    (1 - 13^3) / (1 - 13) = 183

    (1 - 6^3) / (1 - 6) = 43

    (1 - 3^3) / (1 - 3) = 13 # match

n 10^18 (floor(log2 (10^18)) + 1)^2 = 3600 .

+1

: x, 1 b, x = b^n + b^(n-1) + b^(n-2) + ... + b^1 + 1

1 , , b: b^n + b^(n-1) + b^(n-2) + ... + b^1, 111...110. b , b^(n-1) + b^(n-2) + ... + b^1 111...111 , . , 0.

13, 111 3:

13 - 1 = 12 --> 110
12 / 3 = 4  -->  11
 4 - 1 = 3  -->  10
 3 / 3 = 1  -->   1
 1 - 1 = 0  -->   0
Done => 13 can be represented as all 1s in base 3

, , 1 b, , b 1. , .

-, , , mod .

+2

Source: https://habr.com/ru/post/1660757/


All Articles