Line
InterviewTime limit0.5sMemory limit1024 MB
Late students repeatedly cut in one between every pair, and given the final count N, find the smallest possible initial number of students.
- Level
Medium4 of 10
- Topics
- Math, Implementation, Brute force, Number theory
- Solved
- No attempts yet
Problem
Several students have lined up one behind another, waiting for covid testing to begin. Suddenly more students arrive late and cut in, exactly one of them standing between every two students in the line. After a while more late students arrive and do the same: they cut in, one standing between every two students in the line. This repeats several times. In the end, right before covid testing begins, there are N students in the line.
Write a program line that finds the smallest possible number of students that could have been in the line initially.
Input
The first line of the standard input contains a single integer N, equal to the number of students in the line right before covid testing begins.
Output
The first line of the standard output must contain a single integer equal to the smallest number of students that could have been in the line initially.
Constraints
- 1 < N < 10 000
Hint
Explanation of sample 1: The students who arrived in the last group placed themselves exactly one between the lined-up students. Since the line of 29 students has those who cut in distributed one apart, the number of students before that was 15 and the last group had 14 students. Thus, before the last group arrived, the line had 15 students. It follows that the group before that consisted of 7 students. Before they came to the line there were 8 students. This is also the initial number of students, because 8 students cannot be split into two groups such that the students of one group cut in one between every pair of students of the other group.