[ICPC 2021 Macao R] Pass the Ball!

Luogu
IDLGP9660
Time2000ms
Memory256MB
DifficultyP6
2021多项式O2优化快速傅里叶变换 FFT快速数论变换 NTTICPC澳门
There are $n$ children playing with $n$ balls. Both children and balls are numbered from $1$ to $n$. Before the game, $n$ integers $p_1, p_2, \cdots, p_n$ are given. In each round of the game, child $i$ will pass the ball he possesses to child $p_i$. It is guaranteed that no child will pass his ball to himself, which means $p_i \neq i$. Moreover, we also know that after each round, each child will hold exactly one ball. Let $b_i$ be the ball possessed by child $i$. At the beginning of the game, child $i$ ($1 \le i \le n$) will be carrying ball $i$, which means $b_i=i$ initially. You're asked to process $q$ queries. For each query you're given an integer $k$ and you need to compute the value of $\sum\limits_{i=1}^{n} i \times b_i$ after $k$ rounds. ## Input There is only one test case for each test file. The first line of the input contains two integers $n$ ($2 \le n \le 10^5$) and $q$ ($1 \le q \le 10^5$), indicating the number of children and the number of queries. The second line contains $n$ integers $p_1, p_2, \cdots, p_n$ ($1 \le p_i \le n$) indicating how the children pass the balls around. For the following $q$ lines, the $i$-th line contains one integer $k_i$ ($1 \le k_i \le 10^9$) indicating a query asking for the result after $k_i$ rounds. ## Output For each query output one line containing one integer indicating the answer. [samples] ## Note 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} $$
Samples
Input #1
4 4
2 4 1 3
1
2
3
4
Output #1
25
20
25
30
API Response (JSON)
{
  "problem": {
    "name": "[ICPC 2021 Macao R] Pass the Ball!",
    "description": {
      "content": "There are $n$ children playing with $n$ balls. Both children and balls are numbered from $1$ to $n$. Before the game, $n$ integers $p_1, p_2, \\cdots, p_n$ are given. In each round of the game, child ",
      "description_type": "Markdown"
    },
    "platform": "Luogu",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": {
      "LuoguStyle": "P6"
    },
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "LGP9660"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "There are $n$ children playing with $n$ balls. Both children and balls are numbered from $1$ to $n$.\n\nBefore the game, $n$ integers $p_1, p_2, \\cdots, p_n$ are given. In each round of the game, child ...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments