Created
January 25, 2017 07:40
-
-
Save vireshas/ce07d5ff9d6fa577aaa168d1a68df877 to your computer and use it in GitHub Desktop.
Strange Counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Bob has a strange counter. At the first second, , it displays the number . At each subsequent second, the number displayed by the counter decrements by . | |
The counter counts down in cycles. In the second after the counter counts down to , the number becomes the initial number for that countdown cycle and then continues counting down from the new initial number in a new cycle. The diagram below shows the counter values for each time in the first three cycles: | |
https://s3.amazonaws.com/hr-challenge-images/22185/1469447349-bae87a5071-strange1.png | |
Given a time, , find and print the value displayed by the counter at time . | |
Input Format | |
A single integer denoting the value of . | |
Constraints | |
Subtask | |
for of the maximum score. | |
Output Format | |
Print the value displayed by the strange counter at the given time . | |
Sample Input | |
4 | |
Sample Output | |
6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/ruby | |
t = gets.strip.to_i - 1 | |
s = 3 | |
while s <= t | |
t -= s | |
s *= 2 | |
end | |
puts s - t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment