QOJ.ac

QOJ

Time Limit: 3.0 s Memory Limit: 256 MB Total points: 100

#13189. 排列

Statistics

大小为 $N$ 的置换 $P$ 定义为一个数组 $[P_1, P_2, \dots, P_N]$,其中 $1 \le P_i \le N$ 且对于 $i \neq j$ 有 $P_i \neq P_j$。

我们定义置换的顺序。如果 $A$ 和 $B$ 是大小为 $N$ 的置换,则称 $A$ 小于 $B$,当且仅当存在一个索引 $i$ ($1 \le i \le N$) 满足: $A_i < B_i$,且 对于所有 $1 \le j < i$,有 $A_j = B_j$。

我们定义两个置换的乘法。如果 $A$ 和 $B$ 是大小为 $N$ 的置换,则 $A \times B$ 是一个大小为 $N$ 的置换,其第 $i$ 个元素为 $A_{B_i}$。

我们定义置换的幂运算。如果 $P$ 是一个置换,$z$ 是一个正整数,则 $P^z$ 定义如下: $P^z = P$,当 $z = 1$ 时 $P^z = P^{z-1} \times P$,当 $z > 1$ 时

给定一个大小为 $N$ 的置换 $P$。令 $M$ 为满足 $P = P^M$ 的最小大于 1 的整数。我们定义 $A$(索引从 1 开始)为一个数组,包含所有 $P^i$(其中 $1 \le i < M$),并按置换的递增顺序排序。换句话说,$A_i < A_j$ 对于所有 $1 \le i < j < M$ 成立。

例如,假设 $P = [2,3,1,5,4]$。因此: $P^1 = [2,3,1,5,4]$ $P^2 = [3,1,2,4,5]$ $P^3 = [1,2,3,5,4]$ $P^4 = [2,3,1,4,5]$ $P^5 = [3,1,2,5,4]$ $P^6 = [1,2,3,4,5]$ * $P^7 = [2,3,1,5,4]$

因此,本例中 $M$ 的值为 7,且 $A = [P^6, P^3, P^4, P^1, P^2, P^5]$。

你还需要回答 $Q$ 个查询。第 $i$ 个查询包含一个整数 $K_i$。第 $i$ 个查询的答案是一个整数 $T_i$,满足 $1 \le T_i < M$ 且 $P^{T_i} = A_{K_i}$。你能回答所有查询吗?

输入格式

第一行包含两个整数:$N$ ($1 \le N \le 100$) 和 $Q$ ($1 \le Q \le 300,000$),表示置换的大小和查询的数量。第二行包含 $N$ 个整数:$P_1, P_2, \dots, P_N$ ($1 \le P_i \le N$),表示该置换。保证对于所有 $i \neq j$,$P_i \neq P_j$。接下来的 $Q$ 行,每行包含一个整数;第 $i$ 行的整数是 $K_i$ ($1 \le K_i < M$,其中 $M$ 是满足 $P = P^M$ 的最小大于 1 的整数,如上所述。注意 $M$ 在本题中未显式给出),表示查询。

输出格式

输出 $Q$ 行,每行包含一个整数 $T_i$,表示第 $i$ 个查询的答案。

样例

样例输入 1

5 6 
2 3 1 5 4 
1 
2 
3 
4 
5 
6

样例输出 1

6 
3 
4 
1 
2 
5

说明

第一个样例中的置换与题目描述中给出的置换相同。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.