Corridor
Time limit1sMemory limit2048 MB
Given N corridor segments with one-use teleporters that send David back to earlier segments, find his total walking time to exit, modulo 10^9+7.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Prefix sum
- Solved
- No attempts yet
Statement
David wants to leave a secret science laboratory. He is at the far end of a corridor from the exit, so he has to walk the entire length of the corridor.
The corridor has segments. Some segments contain a teleporter.
Each teleporter sends whatever enters it to a fixed target segment. The target is always farther from the exit than the teleporter, so it is closer to David's starting point. Each teleporter can be switched on or off.
When David enters a segment with a switched-on teleporter, whether by walking or by teleportation, he is teleported to the target segment, and that teleporter switches off. When he enters a segment whose teleporter is off, he is not teleported, and the teleporter switches on.
All teleporters are on at the start.
Find how long it takes David to leave the laboratory.
Walking from one segment to the next takes one second. Teleportation is instantaneous. David always walks toward the exit. Leaving from the last segment also takes one second.
Input
The first line contains one integer (), the number of segments.
The second line contains integers (), the teleporter targets. If , segment is empty. Otherwise, segment has a teleporter that sends David to segment .
David starts at segment 1.
Output
Print David's time to exit the laboratory, modulo .