CF1411F.The Thorny Path

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

According to a legend the Hanoi Temple holds a permutation of integers from 11 to nn . There are nn stones of distinct colors lying in one line in front of the temple. Monks can perform the following operation on stones: choose a position ii ( 1in1 \le i \le n ) and cyclically shift stones at positions ii , p[i]p[i] , p[p[i]]p[p[i]] , .... That is, a stone from position ii will move to position p[i]p[i] , a stone from position p[i]p[i] will move to position p[p[i]]p[p[i]] , and so on, a stone from position jj , such that p[j]=ip[j] = i , will move to position ii .

Each day the monks must obtain a new arrangement of stones using an arbitrary number of these operations. When all possible arrangements will have been obtained, the world will end. You are wondering, what if some elements of the permutation could be swapped just before the beginning? How many days would the world last?

You want to get a permutation that will allow the world to last as long as possible, using the minimum number of exchanges of two elements of the permutation.

Two arrangements of stones are considered different if there exists a position ii such that the colors of the stones on that position are different in these arrangements.

输入格式

Each test consists of multiple test cases. The first line contains the number of test cases tt ( 1t1031 \leq t \leq 10^3 ). Description of the test cases follows.

The first line of each test case contains nn ( 3n1063 \leq n \leq 10^6 ). The next line contains nn integers p1,,pnp_1, \dots, p_n ( 1pin1 \le p_i \le n ). It is guaranteed that pp is a permutation.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6 .

输出格式

For each of the tt test cases, print two integers on a new line: the largest possible number of days the world can last, modulo 109+710^9 + 7 , and the minimum number of exchanges required for that.

输入输出样例

  • 输入#1

    3
    3
    2 3 1
    3
    2 1 3
    3
    1 2 3

    输出#1

    3 0
    3 1
    3 2
  • 输入#2

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

    输出#2

    4 0
    4 1
    4 0
    4 1
    4 2

说明/提示

Let's label the colors of the stones with letters. Explanations for the first two test cases of the first example:

  1. Using the permutation [2,3,1][2, 3, 1] , we can additionally obtain the arrangements CAB and BCA from ABC. This is already the maximum possible result.
  2. Using the permutation [2,1,3][2, 1, 3] , the only BAC can be obtained from ABC. As we saw in the previous case, two arrangements are not the maximum possible number of distinct arrangements for n=3n = 3 . To get an optimal permutation, for example, we can swap 11 and 33 , so we will get the permutation [2,3,1][2, 3, 1] .
首页