#P1062. K-Power Sum Sequence
K-Power Sum Sequence
K-Power Sum Sequence
Given a positive integer \( k \) (where \( 3 \le k \le 15 \)), consider the sequence formed by all the powers of \( k \) and all possible sums of distinct powers of \( k \). The sequence is sorted in increasing order. For example, when \( k = 3 \), the sequence is:
\(1, 3, 4, 9, 10, 12, 13, \ldots\)
This sequence can be seen as all the numbers whose base-\( k \) representation contains only the digits 0 and 1. In other words, the \( N \)th term of the sequence can be calculated by writing \( N \) in binary and interpreting that binary number as a number in base \( k \). For example, when \( k = 3 \) and \( N = 100 \), the binary representation of 100 is \( 1100100 \). Interpreting \( 1100100 \) as a base-3 number gives:
\(1\times3^6 + 1\times3^5 + 0\times3^4 + 0\times3^3 + 1\times3^2 + 0\times3^1 + 0\times3^0 = 729 + 243 + 0 + 0 + 9 + 0 + 0 = 981\).
Your task is to compute and output the \( N \)th term of this sequence in decimal notation.
inputFormat
The input consists of two space-separated integers: \( k \) (\( 3 \le k \le 15 \)) and \( N \) (\( N \ge 1 \)), where \( N \) represents the position in the sequence.
outputFormat
Output a single integer, which is the \( N \)th term of the sequence.
sample
3 100
981