Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Lithuanian Olympiad in Informatics

Country round (2) • 2023 March 23’rd-26’th • VIII–XII g. koridorius

Long Corridor
Dovydas wants to exit from a secret science laboratory. He is in a corridor on the opposite
end from the exit, so he will have to walk along the entire length of the corridor.
It would be straightforward to leave, but some teleporters are being tested in the corridor.
The corridor consists of N segments. Some segments contain a teleporter.
Each teleporter is configured to teleport stuff into a particular target segment. The segment
is always further from the exit than the teleporter (so closer to Dovydas’s starting position).
Also each teleporter may be switched on or off.
When Dovydas enters (either walks or is teleported into) a segment with a turned on tele-
porter, he is teleported into the target segment, and the teleporter turns off. When Dovydas
enters a segment with a turned off teleporter, he is not teleported, but the teleporter turns
on.
All teleporters are turned on initially.

Task. Find how long it will take for Dovydas to leave the laboratory.
It takes one second for Dovydas to get to the next segment. Teleportation is instantaneous.
Dovydas will always walk towards the exit. Exiting the corridor from the last segment also
takes one second.
Since the answer may be very large, output the total travel time in seconds modulo 109 + 7 1 .

Input. The first line contains one integer N – the number of segments the corridor consists
of.
The second line contains N integers a1 , a2 , ..., aN denoting the teleporter targets.
If ai = i, then i-th segment is empty. Otherwise i-th segment contains a teleporter which
teleports stuff into the ai -th segment.
Dovydas starts at the 1-st segment.

Output. Output one integer – Dovydas’s journey duration in seconds modulo 109 + 7.

Examples.

Input Output Explanation


5 10 Dovydas will move in the following way (te-
1 2 2 1 5 leportation is denoted by ⇒): 1 → 2 → 3 ⇒
2→3→4⇒1→2→3⇒2→3→4→
5 → exit
1
In C/C++ you can use a%b operation.

Page 1 of 2
Lithuanian Olympiad in Informatics
Country round (2) • 2023 March 23’rd-26’th • VIII–XII g. koridorius

Input Output Explanation


3 6 Dovydas will move in the following way:
1 1 2 1→2⇒1→2→3⇒2⇒1→2→3→
exit

Input Output Explanation


5 31
1 1 1 1 1

Constraints. The following constraints hold for all tests 1 ≤ N ≤ 100 000 and 1 ≤ ai ≤ i
(for all 1 ≤ i ≤ N ).

Subtasks. Subtask points are only awarded if all tests of that subtask are passed.

No. Points Additional constraints


1 10 1 ≤ N ≤ 20
2 12 At most 20 teleporters exist
3 19 For all 1 ≤ i ≤ N , ai ≥ i − 1
4 21 For all 1 ≤ i ≤ N , ai = i or ai = 1
5 21 For all 1 ≤ i < j ≤ N , if ai 6= i, then aj < ai or aj > i
6 17 No additional constraints

Page 2 of 2

You might also like