Permutation II
时间限制: 2 Sec 内存限制: 128 MB:
题目描述
A permutation P of size N is defined as an array [P1,P2,…,PN] where 1 ≤ Pi ≤ N and Pi ≠ Pj for i≠j.
We also define an order of a permutation. If A and B are permutations of size N , then A is less than B if and only if there exists an index i (1 ≤ i ≤ N ) where:
• Ai<Bi , and
• Aj = Bj for all 1 ≤ j < i
We also define the multiplication of two permutations. If A and B are permutations of size N , then A×B is a permutation of size N , where the i-th element is ABi.
We also define the exponentiation of a permutation and a positive integer. If P is permutation and Z is a positive integer, then PZ is defined as follow:
• PZ = P , for Z = 1
• PZ = PZ-1 × P , for Z > 1
You are given a permutation P of size N . Let M be the smallest integer greater than 1 such that P=PM . We define A (index starts from 1) as an array consisting of Pi for all 1 ≤ i < M sorted in the increasing order (of permutation). In other words, Ai<Aj for all 1 ≤ i < j < M .
For example, suppose P=[2,3,1,5,4]. Therefore:
• P1 = [2,3,1,5,4] ,
• P2 = [3,1,2,4,5] ,
• P3 = [1,2,3,5,4] ,
• P4 = [2,3,1,4,5] ,
• P5 = [3,1,2,5,4],
• P6 = [1,2,3,4,5] ,
• P7 = [2,3,1,5,4] ,
Thus, the value of M in this case is 7, and A=[P6,P3,P4,P1,P2,P5] .
You are also given Q queries. The i-th query contains an integer Ki . The answer for the i-th query is an integer Ti such that 1 ≤ Ti < M and PTi = Aki . Can you answer all of the queries?
输入
The first line contains two integers: N Q(1 ≤ N ≤ 100; 1 ≤ Q ≤ 300,000) in a line denoting the size of the permutation and the number of queries. The second line contains N integers: P1 P2 … PN (1 ≤ Pi ≤ N ) in a line denoting the permutation. It is guaranteed that Pi≠Pj for all i≠j . The next Q lines,each contains an integer; the integer on the i-th line is Ki (1 ≤ Ki < M , where M is the smallest integer greater than 1 such that P=PM as explained above. Note that ! is not explicitly given in this problem) denoting the query
输出
Q lines, each contains an integer: Ti in a line denoting the answer of the i-th query.
样例输入
5 6
2 3 1 5 4
1
2
3
4
5
6
样例输出
6
3
4
1
2
5
提示
The permutation given in the first sample is the same as the permutation given in the problem description.
题意
首先定义两个排列的乘法:若
C
=
A
×
B
C=A\times B
C=A×B ,则对于每一个i,有
C
i
=
A
B
i
C_i=A_{B_i}
Ci=ABi。
然后定义排列的幂:
A
n
=
A
×
A
×
A
×
A
.
.
.
A^n=A\times A\times A\times A...
An=A×A×A×A...(共n个)
定义M为第一个满足
A
M
=
A
A^M=A
AM=A的,大于1的数。
然后输入一个排列P,将P的所有小于M的幂次按照字典序排序,得到P。
现在有Q次询问,每次询问一个整数K,在
[
1
,
M
)
\left[1,M\right)
[1,M)上求一个整数T使得
P
T
i
=
A
k
i
P^{T_i }= A_{k_i}
PTi=Aki
思路
定义的乘法变换可以形象的理解为传送带。
有一些排列可以通过乘法变换使得该排列的幂次中每个数都可以在不同的位置出现一次,如2,3,1:
1次:2,3,1
2次:3,1,2
3次:1,2,3
4次:2,3,1,恢复到了初始状态,故只有三个幂次。这三个排列中,1,2,3分别处于不同的位置,且都出现了一次。
其他的排列可以被拆分成多个子排列,这些子排列满足上述的条件,如样例中的2,3,1,5,4可以拆分成2,3,1和5,4。
对于第一类排列,循环节(即M值)为排列长度n;
对于第二类排列,循环节为它的每个子排列的循环节取一下lcm(最小公倍数)
至于排列的排序,可以交给sort搞定。
可以算出最坏的情况,循环节可以达到
2
×
3
×
5
×
7
×
11
×
13
×
17
×
19
×
23
2\times 3\times 5\times 7\times 11 \times 13 \times 17 \times 19 \times 23
2×3×5×7×11×13×17×19×23左右,大概到了2e8的程度,加上每个排列有100的空间开销,所以暴力枚举自然MLE,即使解决了内存问题也会TLE,所以需要寻找其他的做法。
对于第二类的排列,可以将其拆分成多个第一类的排列,对每一个分别枚举并且排序,最终组合出所求的答案。