Jump Jump 2
InterviewTime limit2sMemory limit512 MB
Given a walk that adds or subtracts A_i at each step, count the stones reachable from start s, excluding s unless a cycle returns to it.
- Level
Medium5 of 10
- Topics
- Graph, BFS, Implementation, Math
- Solved
- No attempts yet
Problem
Youngwoo the frog is on a stone bridge made of stones in a row. The stones are numbered to from the left, and stone has a single number written on it. From stone Youngwoo can jump exactly stones to the left or to the right, landing on stone or on stone . He cannot jump to a position numbered below or above .
Youngwoo starts on stone . There is no limit on the number of jumps, and he may land on the same stone more than once. Call a stone visitable if Youngwoo can land on it after one or more jumps. The starting stone is visitable only when he can land back on it after at least one jump.
Count the visitable stones.
Input
The first line contains the number of stones ().
The second line contains , separated by spaces ().
The third line contains the starting position ().
Output
Print the number of visitable stones on the first line.