Infinite Adventure
Time limit2sMemory limit1024 MB
Given portals whose cycle times are powers of 2, find the city reached after Delta portal hops from city v starting on day t.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Math
- Solved
- No attempts yet
Problem
Bessie is planning an infinite adventure in a land with () cities. Each city has a portal and a cycling time . Every is a power of , and . If you enter city 's portal on day , you instantly exit in city . Bessie has () plans, each a tuple . In each plan she starts in city on day , then repeats the following times: she follows the portal in her current city, then waits one day. For each plan, find the city where she ends up.
Input
The first line contains two integers and , separated by a space. The second line contains , separated by spaces (, each is a power of , and ). For , line contains , separated by spaces (). For , line contains three positive integers separated by spaces (, , ).
Output
Print lines. The th line is the answer to the th plan, the city where Bessie ends up.