#P9660. [ICPC2021 Macao R] Pass the Ball!

    ID: 8977 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 6 Uploaded By: Tags>2021多项式O2优化快速傅里叶变换 FFT快速数论变换 NTTICPC澳门

[ICPC2021 Macao R] Pass the Ball!

题目描述

There are nn children playing with nn balls. Both children and balls are numbered from 11 to nn.

Before the game, nn integers p1,p2,,pnp_1, p_2, \cdots, p_n are given. In each round of the game, child ii will pass the ball he possesses to child pip_i. It is guaranteed that no child will pass his ball to himself, which means piip_i \neq i. Moreover, we also know that after each round, each child will hold exactly one ball.

Let bib_i be the ball possessed by child ii. At the beginning of the game, child ii (1in1 \le i \le n) will be carrying ball ii, which means bi=ib_i=i initially. You're asked to process qq queries. For each query you're given an integer kk and you need to compute the value of i=1ni×bi\sum\limits_{i=1}^{n} i \times b_i after kk rounds.

输入格式

There is only one test case for each test file.

The first line of the input contains two integers nn (2n1052 \le n \le 10^5) and qq (1q1051 \le q \le 10^5), indicating the number of children and the number of queries.

The second line contains nn integers p1,p2,,pnp_1, p_2, \cdots, p_n (1pin1 \le p_i \le n) indicating how the children pass the balls around.

For the following qq lines, the ii-th line contains one integer kik_i (1ki1091 \le k_i \le 10^9) indicating a query asking for the result after kik_i rounds.

输出格式

For each query output one line containing one integer indicating the answer.

题目大意

【题目描述】

nn 个孩子和 nn 个球在玩游戏。孩子和球都从 11 编号到 nn

游戏开始前,给出了 nn 个整数 p1,p2,,pnp_1, p_2, \cdots, p_n。在游戏的每一轮中,孩子 ii 会把他手里的球传给孩子 pip_i。保证没有孩子会把他手里的球传给自己,也就是说 piip_i \neq i。此外,我们还知道在每一轮之后,每个孩子手里都会正好持有一个球。

bib_i 表示孩子 ii 所持有的球。在游戏开始时,孩子 ii1in1 \le i \le n)将携带球 ii,也就是说 bi=ib_i=i。你需要处理 qq 个查询。对于每个查询,你会得到一个整数 kk,你需要计算在 kk 轮后 i=1ni×bi\sum\limits_{i=1}^{n} i \times b_i 的值。

【输入格式】

输入的第一行包含两个整数 nn2n1052 \le n \le 10^5)和 qq1q1051 \le q \le 10^5),表示孩子的数量和查询的数量。

第二行包含 nn 个整数 p1,p2,,pnp_1, p_2, \cdots, p_n1pin1 \le p_i \le n),表示孩子之间传球的方式。

接下来的 qq 行中,第 ii 行包含一个整数 kik_i1ki1091 \le k_i \le 10^9),表示询问在 kik_i 轮后的结果。

【输出格式】

对于每个查询,输出一行包含一个整数,表示答案。

【样例解释】

示例测试用例解释如下。

$$\begin{array}{|c|c|c|c|c|c|} \hline \textbf{轮次} & \textbf{b1} & \textbf{b2} & \textbf{b3} & \textbf{b4} & \textbf{答案} \\\hline 1 & 3 & 1 & 4 & 2 & 25 \\\hline 2 & 4 & 3 & 2 & 1 & 20 \\\hline 3 & 2 & 4 & 1 & 3 & 25 \\\hline 4 & 1 & 2 & 3 & 4 & 30 \\\hline \end{array} $$

翻译来自于:ChatGPT

4 4
2 4 1 3
1
2
3
4
25
20
25
30

提示

The sample test case is explained below.

$$\begin{array}{|c|c|c|c|c|c|} \hline \textbf{Round} & \textbf{b1} & \textbf{b2} & \textbf{b3} & \textbf{b4} & \textbf{Answer} \\\hline 1 & 3 & 1 & 4 & 2 & 25 \\\hline 2 & 4 & 3 & 2 & 1 & 20 \\\hline 3 & 2 & 4 & 1 & 3 & 25 \\\hline 4 & 1 & 2 & 3 & 4 & 30 \\\hline \end{array} $$