Strange Counter

Bob has a strange counter. At the first second, it displays the number . Each second, the number displayed by the counter decrements by  until it reaches .
The counter counts down in cycles. In next second, the timer resets to  and continues counting down. The diagram below shows the counter values for each time  in the first three cycles:
Find and print the value displayed by the counter at time .
Function Description
Complete the strangeCounter function in the editor below. It should return the integer value displayed by the counter at time .
strangeCounter has the following parameter(s):
  • t: an integer
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
Explanation
Time  marks the beginning of the second cycle. It is double the number displayed at the beginning of the first cycle:. This is also shown in the diagram in the Problem Statement above.

php
<?php

// Complete the strangeCounter function below.
function strangeCounter($t) {
$c = 1;
$f = 1;
$s = 3;
$l = 3;
while($s < $t) {
$c++;
$l = $l*2;
$f = $s+1;
$s = $f + $l - 1;
//print_r($s.'-');
}
//print_r($s);
return $s - $t +1;
}

$fptr = fopen(getenv("OUTPUT_PATH"), "w");

$stdin = fopen("php://stdin", "r");

fscanf($stdin, "%ld\n", $t);

$result = strangeCounter($t);

fwrite($fptr, $result . "\n");

fclose($stdin);
fclose($fptr);

Comments

Popular posts from this blog

Intro to Tutorial Challenges

Mini-Max Sum