#P8629. Initial Robot Count Recovery in X Galaxy
Initial Robot Count Recovery in X Galaxy
Initial Robot Count Recovery in X Galaxy
In Galaxy X, each robot can automatically reproduce. In one year, a robot produces 2 copies of itself and then loses its ability to reproduce.
Every year, the galaxy sends exactly one of the newly born robots into space, which is no longer available for further reproduction. In other words, if there are p initial robots, then after the first year the number of newly reproduced robots becomes 2p - 1: each of the p robots produces 2 copies and one of these copies is sent into space. The robots that remain (2p - 1) will reproduce in the following years in the same manner.
Formally, if we denote by \(a_0 = p\) the initial number of robots and for \(k \ge 1\), \(a_k = 2a_{k-1} - 1\) the number of robots newly produced and retained in year \(k\), then after \(n\) years the total number of robots is
\[ S = a_0 + a_1 + \cdots + a_n = p \cdot (2^{n+1}-1) - \Bigl((2^{n+1}-2) - n\Bigr). \]Given the detected total number of robots \(S\) after \(n\) years, your task is to determine the initial number of robots \(p\). The answer is given by the formula:
\[ p = \frac{S + 2^{n+1} - 2 - n}{2^{n+1}-1}. \]You may assume that for the input values provided, \(p\) is an integer.
inputFormat
The input consists of a single line containing two integers n
and S
separated by a space, where n
(\(0 \le n \le 30\)) is the number of years and S
(\(S \ge 1\)) is the total number of robots after n
years.
outputFormat
Output the initial number of robots p
as an integer.
sample
1 8
3