r/programmingcontests • u/moobrip • Dec 11 '23
How to solve this problem?
Bruce recently got a job at NEERC (Numeric Expression Engineering & Research Center), where they study and build many different curious numbers. His first assignment was a study of decimal numbers. A natural number is called a binary number if its decimal representation is a suffix of its binary representation; both the binary and decimal representations are considered without leading zeros. For example, 1010 = 10102, so 10 is a bivariate number. The numbers 101010 = 11111100102 and 4210 = 1010102 are not decimal. First, Bruce wants to create a list of bicentennial numbers. Help him find the nth smallest bicentennial number.
Input
One integer n (1 ≤ n ≤ 10 000).
Output data
Print one number - the n-th smallest bipartite number in decimal representation.
1
u/brownbahun Dec 12 '23
The wording in this question is very confusing, but based on what I understood, here's my solution:
Few things to consider:
We are not interested in any decimal number containing digits other than 0 and 1. That is, we are only interested in decimal numbers such as 0, 1, 10, 11, 100, 101 ....
How to only iterate through such numbers? I think the best way would be to use binary representation of numbers and convert it to decimal number. For example: use binary representation of 2 (which is 10 base 2) and convert this to decimal number 10 (base 10). Similarly, 3 to 11, 4 to 100, 5 to 101... and so on
How to check if this number is bicentennial? Well, let's first understand how decimal and binary representation can be interpreted. For this, we successively divide a number by the base, until it's 0, and keep on appending the reminder. So, if n%2 = n%10 at all times until n is 0, the decimal representation will surely be the suffix of binary representation. We can check this in complexity: O(log(number))
Considering these things, I wrote a C++ code and found out the 10000th such number is 10011100001111, which is less than 2^15 in binary. So we have to iterate only upto 2^15 at max. So the overall complexity will be O(nlogn) where maximum value of n is 2^15, which should be fast enough.
Here's the code, hope this helps