CF1787F.Inverse Transformation

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A permutation scientist is studying a self-transforming permutation aa consisting of nn elements a1,a2,…,ana_1,a_2,\ldots,a_n.

A permutation is a sequence of integers from 11 to nn of length nn containing each number exactly once. For example, [1][1], [4,3,5,1,2][4, 3, 5, 1, 2] are permutations, while [1,1][1, 1], [4,3,1][4, 3, 1] are not.

The permutation transforms day by day. On each day, each element xx becomes axa_x, that is, axa_x becomes aaxa_{a_x}. Specifically:

  • on the first day, the permutation becomes bb, where bx=aaxb_x = a_{a_x};
  • on the second day, the permutation becomes cc, where cx=bbxc_x = b_{b_x};
  • …\ldots

For example, consider permutation a=[2,3,1]a = [2,3,1]. On the first day, it becomes [3,1,2][3,1,2]. On the second day, it becomes [2,3,1][2,3,1].

You're given the permutation a′a' on the kk-th day.

Define σ(x)=ax\sigma(x) = a_x, and define f(x)f(x) as the minimal positive integer mm such that σm(x)=x\sigma^m(x) = x, where σm(x)\sigma^m(x) denotes σ(σ(…σ⏟m times(x)…))\underbrace{\sigma(\sigma(\ldots \sigma}_{m \text{ times}}(x) \ldots)).

For example, if a=[2,3,1]a = [2,3,1], then σ(1)=2\sigma(1) = 2, σ2(1)=σ(σ(1))=σ(2)=3\sigma^2(1) = \sigma(\sigma(1)) = \sigma(2) = 3, σ3(1)=σ(σ(σ(1)))=σ(3)=1\sigma^3(1) = \sigma(\sigma(\sigma(1))) = \sigma(3) = 1, so f(1)=3f(1) = 3. And if a=[4,2,1,3]a=[4,2,1,3], σ(2)=2\sigma(2) = 2 so f(2)=1f(2) = 1; σ(3)=1\sigma(3) = 1, σ2(3)=4\sigma^2(3) = 4, σ3(3)=3\sigma^3(3) = 3 so f(3)=3f(3) = 3.

Find the initial permutation aa such that ∑i=1n1f(i)\sum\limits^n_{i=1} \dfrac{1}{f(i)} is minimum possible.

一位排列科学家正在研究一个自变换排列 aa,它由 nn 个元素 a1,a2,…,ana_1,a_2,\ldots,a_n 构成。

排列是指一个长度为 nn、由 11 到 nn 的整数组成的序列,其中每个数恰好出现一次。例如,[1][1]、[4,3,5,1,2][4, 3, 5, 1, 2] 是排列,而 [1,1][1, 1]、[4,3,1][4, 3, 1] 不是。

该排列逐日发生变换。每天,每个元素 xx 变为 axa_x,即:axa_x 变为 aaxa_{a_x}。具体而言:

  • 第一天,排列变为 bb,其中 bx=aaxb_x = a_{a_x};
  • 第二天,排列变为 cc,其中 cx=bbxc_x = b_{b_x};
  • …\ldots

例如,考虑排列 a=[2,3,1]a = [2,3,1]。第一天它变为 [3,1,2][3,1,2];第二天它变为 [2,3,1][2,3,1]。

你被给定第 kk 天的排列 a′a'。

定义 σ(x)=ax\sigma(x) = a_x,并定义 f(x)f(x) 为满足 σm(x)=x\sigma^m(x) = x 的最小正整数 mm,其中 σm(x)\sigma^m(x) 表示 σ(σ(…σ⏟m 次(x)…))\underbrace{\sigma(\sigma(\ldots \sigma}_{m \text{ 次}}(x) \ldots))。

例如,若 a=[2,3,1]a = [2,3,1],则 σ(1)=2\sigma(1) = 2,σ2(1)=σ(σ(1))=σ(2)=3\sigma^2(1) = \sigma(\sigma(1)) = \sigma(2) = 3,σ3(1)=σ(σ(σ(1)))=σ(3)=1\sigma^3(1) = \sigma(\sigma(\sigma(1))) = \sigma(3) = 1,因此 f(1)=3f(1) = 3;又如 a=[4,2,1,3]a=[4,2,1,3],有 σ(2)=2\sigma(2) = 2,故 f(2)=1f(2) = 1;σ(3)=1\sigma(3) = 1,σ2(3)=4\sigma^2(3) = 4,σ3(3)=3\sigma^3(3) = 3,故 f(3)=3f(3) = 3。

请找出初始排列 aa,使得 ∑i=1n1f(i)\sum\limits^n_{i=1} \dfrac{1}{f(i)} 达到可能的最小值。

输入格式

Each test contains multiple test cases. The first line contains an integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains two integers nn and kk (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5; 1≤k≤1091 \le k \le 10^9) — the length of aa, and the last day.

The second line contains nn integers a1′,a2′,…,an′a'_1,a'_2,\ldots,a'_n (1≤ai′≤n1 \le a'_i \le n) — the permutation on the kk-th day.

It's guaranteed that the sum of nn does not exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例的第一行包含两个整数 nn 和 kk(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5;1≤k≤1091 \le k \le 10^9)—— 数组 aa 的长度,以及最后一天的编号。

每个测试用例的第二行包含 nn 个整数 a1′,a2′,…,an′a'_1,a'_2,\ldots,a'_n(1≤ai′≤n1 \le a'_i \le n)—— 第 kk 天的排列。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, if at least one initial aa consistent with the given a′a' exists, print "YES", then print nn integers a1,a2,…,ana_1,a_2,\ldots,a_n — the initial permutation with the smallest sum ∑i=1n1f(i)\sum\limits^n_{i=1} \dfrac{1}{f(i)}. If there are multiple answers with the smallest sum, print any.

If there are no valid permutations, print "NO".

对于每个测试用例,若存在至少一个与给定的 a′a' 一致的初始 aa,则输出 "YES",然后输出 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n —— 即满足条件且和 ∑i=1n1f(i)\sum\limits^n_{i=1} \dfrac{1}{f(i)} 最小的初始排列。若存在多个具有最小和的解,输出任意一个即可。

若不存在合法排列,则输出 "NO"。

输入输出样例

  • 输入#1

    10
    5 3
    1 2 3 4 5
    7 2
    1 2 3 4 5 6 7
    8 998
    1 2 3 4 5 6 7 8
    6 1
    6 3 5 4 1 2
    4 8
    4 2 1 3
    9 1
    1 5 4 8 7 6 3 2 9
    5 9999999
    2 3 4 5 1
    7 97843220
    4 6 1 2 7 5 3
    3 1000000000
    2 1 3
    12 3
    8 9 10 1 5 3 11 4 7 6 12 2

    输出#1

    YES
    2 3 4 1 5
    YES
    6 2 5 7 1 3 4
    YES
    2 3 4 5 6 7 8 1
    YES
    3 1 6 4 2 5
    YES
    4 2 1 3
    NO
    YES
    3 4 5 1 2
    YES
    2 5 4 6 3 7 1
    NO
    YES
    3 7 8 6 5 1 12 10 11 4 2 9

说明/提示

In the second test case, the initial permutation can be a=[6,2,5,7,1,3,4]a = [6,2,5,7,1,3,4], which becomes [3,2,1,4,6,5,7][3,2,1,4,6,5,7] on the first day and a′=[1,2,3,4,5,6,7]a' = [1,2,3,4,5,6,7] on the second day (the kk-th day). Also, among all the permutations satisfying that, it has the minimum ∑i=1n1f(i)\sum\limits^n_{i=1} \dfrac{1}{f(i)}, which is 14+11+14+12+14+14+12=3\dfrac{1}{4}+\dfrac{1}{1}+\dfrac{1}{4}+\dfrac{1}{2}+\dfrac{1}{4}+\dfrac{1}{4}+\dfrac{1}{2}=3.

In the fifth test case, the initial permutation can be a=[4,2,1,3]a = [4,2,1,3], which becomes [3,2,4,1][3,2,4,1] on the first day, [4,2,1,3][4,2,1,3] on the second day, and so on. So it finally becomes a′=[4,2,1,3]a' = [4,2,1,3] on the 88-th day (the kk-th day). And it has the minimum ∑i=1n1f(i)=13+11+13+13=2\sum\limits^n_{i=1} \dfrac{1}{f(i)} = \dfrac{1}{3} + \dfrac{1}{1} + \dfrac{1}{3} + \dfrac{1}{3} = 2.

在第二个测试用例中,初始排列可以是 a=[6,2,5,7,1,3,4]a = [6,2,5,7,1,3,4],它在第一天变为 [3,2,1,4,6,5,7][3,2,1,4,6,5,7],在第二天(即第 kk 天)变为 a′=[1,2,3,4,5,6,7]a' = [1,2,3,4,5,6,7]。此外,在所有满足条件的排列中,该排列使得 ∑i=1n1f(i)\sum\limits^n_{i=1} \dfrac{1}{f(i)} 最小,其值为 14+11+14+12+14+14+12=3\dfrac{1}{4}+\dfrac{1}{1}+\dfrac{1}{4}+\dfrac{1}{2}+\dfrac{1}{4}+\dfrac{1}{4}+\dfrac{1}{2}=3。

在第五个测试用例中,初始排列可以是 a=[4,2,1,3]a = [4,2,1,3],它在第一天变为 [3,2,4,1][3,2,4,1],第二天变为 [4,2,1,3][4,2,1,3],依此类推。因此,它最终在第 88 天(即第 kk 天)变为 a′=[4,2,1,3]a' = [4,2,1,3]。此时,∑i=1n1f(i)\sum\limits^n_{i=1} \dfrac{1}{f(i)} 取得最小值,为 13+11+13+13=2\dfrac{1}{3} + \dfrac{1}{1} + \dfrac{1}{3} + \dfrac{1}{3} = 2。

输入解题思路,AI测评打分。不知道怎么写?

首页