A12947 | Function Composition
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $F$ on the array $A$ such that $F(i, 1) = A[i]$ and $F(i, m) = A[F(i, m - 1)]$ for $m > 1$ . In other words, value $F(i, m)$ represents composition $A[...A[i]]$ applied $m$ times.
You are given an array of length $N$ with non-negative integers. You are expected to give an answer on $Q$ queries. Each query consists of two numbers – $m$ and $y$ . For each query determine how many $x$ exist such that $F(x,m) = y$ .
First, let us define several things. We define function $F$ on the array $A$ such that $F(i, 1) = A[i]$ and $F(i, m) = A[F(i, m - 1)]$ for $m > 1$ . In other words, value $F(i, m)$ represents composition $A[...A[i]]$ applied $m$ times.
You are given an array of length $N$ with non-negative integers. You are expected to give an answer on $Q$ queries. Each query consists of two numbers – $m$ and $y$ . For each query determine how many $x$ exist such that $F(x,m) = y$ .
输入格式
The first line contains one integer $N$ $(1 \leq N \leq 2 \cdot 10^5)$ – the size of the array $A$ . The next line contains $N$ non-negative integers – the array $A$ itself $(1 \leq A_i \leq N)$ . The next line contains one integer $Q$ $(1 \leq Q \leq 10^5)$ – the number of queries. Each of the next $Q$ lines contain two integers $m$ and $y$ $(1 \leq m \leq 10^{18}, 1\leq y \leq N)$ .
输出格式
Output exactly $Q$ 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) = 1$ and $F(10, 10) = 1$ .
For the second query no $x$ satisfies condition $F(x, 5) = 7$ .
For the third query $F(5, 10) = 6$ holds.
For the fourth query $F(3, 1) = 1.$
For the fifth query no $x$ satisfies condition $F(x, 10) = 8$ .
For the second query no $x$ satisfies condition $F(x, 5) = 7$ .
For the third query $F(5, 10) = 6$ holds.
For the fourth query $F(3, 1) = 1.$
For the fifth query no $x$ satisfies condition $F(x, 10) = 8$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted