The Site of Sunrin
Time limit1sMemory limit512 MB
Find the N-th smallest natural number whose base-3 digits are all 0 or 1, for up to 1000 queries with N up to about 1.2e11.
- Level
Medium4 of 10
- Topics
- Math, Combinatorics, Bit manipulation, Implementation
- Solved
- No attempts yet
Problem
Standing tall on high Namsan
(omitted)
Build it upon bedrock
the site of Sunrin
In 1899, you receive a decree from Emperor Gojong of the Korean Empire and must choose the site on which to build the Government School of Industry and Commerce, Korea's first vocational education institution.
The Korean Empire has several sites suitable for building a school, and each site has a distinct natural number as its number. In particular, the number of the site of Sunrin is a natural number obtained by adding at most one natural number of the form for each . That is, the numbers of the site of Sunrin include , and so on.
You have been ordered to find the -th smallest site of Sunrin. Write a program that finds the -th site of Sunrin.
Input
The first line gives , the number of sites of Sunrin you must find.
Each of the next lines gives , the information about one site of Sunrin you must find.
Output
Print the numbers of the sites of Sunrin you must find, one per line, in order.
Constraints
Hint
The numbers in the input and the answers you must print are very large, so you need a 64-bit type (long long in C/C++, long in Java, printed with %lld).