The Hungry Muto
Time limit1.5sMemory limit512 MB
Given an evolution map on n ingredients, answer Q queries: count starting ingredients x where one evolution step gives the same result as c evolution steps.
- Level
Medium5 of 10
- Topics
- Graph, Number theory
- Solved
- No attempts yet
Problem
Muto often gathers ingredients and cooks meals to eat. Today he was very hungry as usual. Nearby, he saw different Pokemon ingredients numbered from to . Evolving an ingredient once turns it into another Pokemon ingredient. Bored while cooking, Muto asked you the following question times.
Pidori and Pimi start a game with ingredient . Pidori evolves ingredient once, and Pimi evolves ingredient times. Count the number of values of for which Pidori's result equals Pimi's result.
Solve the questions.
Input
The first line contains the number of ingredients . The second line contains integers separated by spaces. The -th integer () means that when ingredient evolves once, it becomes ingredient . The ingredient before and after evolution may be the same.
The next line contains the number of questions . The next lines each contain a value , which means that equals in that question.
Output
Print the answer to each of Muto's questions on its own line.
Constraints