CF1218H.Function Composition

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

We are definitely not going to bother you with another generic story when Alice finds about an array or when Alice and Bob play some stupid game. This time you'll get a simple, plain text.

First, let us define several things. We define function FF on the array AA such that F(i,1)=A[i]F(i, 1) = A[i] and F(i,m)=A[F(i,m1)]F(i, m) = A[F(i, m - 1)] for m>1m > 1 . In other words, value F(i,m)F(i, m) represents composition A[...A[i]]A[...A[i]] applied mm times.

You are given an array of length NN with non-negative integers. You are expected to give an answer on QQ queries. Each query consists of two numbers – mm and yy . For each query determine how many xx exist such that F(x,m)=yF(x,m) = y .

输入格式

The first line contains one integer NN (1N2105)(1 \leq N \leq 2 \cdot 10^5) – the size of the array AA . The next line contains NN non-negative integers – the array AA itself (1AiN)(1 \leq A_i \leq N) . The next line contains one integer QQ (1Q105)(1 \leq Q \leq 10^5) – the number of queries. Each of the next QQ lines contain two integers mm and yy (1m1018,1yN)(1 \leq m \leq 10^{18}, 1\leq y \leq N) .

输出格式

Output exactly QQ lines with a single integer in each that represent the solution. Output the solutions in the order the queries were asked in.

输入输出样例

  • 输入#1

    10
    2 3 1 5 6 4 2 10 7 7
    5
    10 1
    5 7
    10 6
    1 1
    10 8
    

    输出#1

    3
    0
    1
    1
    0
    

说明/提示

For the first query we can notice that F(3,10)=1, F(9,10)=1F(3, 10) = 1,\ F(9, 10) = 1 and F(10,10)=1F(10, 10) = 1 .

For the second query no xx satisfies condition F(x,5)=7F(x, 5) = 7 .

For the third query F(5,10)=6F(5, 10) = 6 holds.

For the fourth query F(3,1)=1.F(3, 1) = 1.

For the fifth query no xx satisfies condition F(x,10)=8F(x, 10) = 8 .

首页