The Thorny Path (Codeforces 1464D)
Tags: Greedy, implementation

According to a legend the Hanoi Temple holds a permutation of integers from $$$1$$$ to $$$n$$$. There are $$$n$$$ stones of distinct colors lying in one line in front of the temple. Monks can perform the following operation on stones: choose a position $$$i$$$ ($$$1 \le i \le n$$$) and cyclically shift stones at positions $$$i$$$, $$$p[i]$$$, $$$p[p[i]]$$$, .... That is, a stone from position $$$i$$$ will move to position $$$p[i]$$$, a stone from position $$$p[i]$$$ will move to position $$$p[p[i]]$$$, and so on, a stone from position $$$j$$$, such that $$$p[j] = i$$$, will move to position $$$i$$$.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 $$$i$$$ such that the colors of the stones on that position are different in these arrangements.

Input: Each test consists of multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^3$$$). Description of the test cases follows.The first line of each test case contains $$$n$$$ ($$$3 \leq n \leq 10^6$$$). The next line contains $$$n$$$ integers $$$p_1, \dots, p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that $$$p$$$ is a permutation.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. 

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

Note: Let's label the colors of the stones with letters. Explanations for the first two test cases of the first example:   Using the permutation $$$[2, 3, 1]$$$, we can additionally obtain the arrangements CAB and BCA from ABC. This is already the maximum possible result.  Using the permutation $$$[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 = 3$$$. To get an optimal permutation, for example, we can swap $$$1$$$ and $$$3$$$, so we will get the permutation $$$[2, 3, 1]$$$. 

Sample Inputs:
3
3
2 3 1
3
2 1 3
3
1 2 3

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

Sample Outputs:
3 0
3 1
3 2

4 0
4 1
4 0
4 1
4 2

Solution (Python):
import sys, io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
outL = []
MOD = 10 ** 9 + 7
for _ in range(t):
    n = int(input())
    p = list(map(lambda x: int(x) - 1, input().split()))
    if n % 3 == 0:
        ans = pow(3, n // 3, MOD)
    if n % 3 == 1:
        ans = pow(3, (n - 4) // 3, MOD) * 4
    if n % 3 == 2:
        ans = 2 * pow(3, (n - 2) // 3, MOD)
    ans %= MOD
    seen = [False] * n
    cycles = []
    for i in range(n):
        if seen[i]:
            continue
        count = 0
        curr = i
        while not seen[curr]:
            seen[curr] = True
            count += 1
            curr = p[curr]
        cycles.append(count)
    if n % 3 == 0:
        out = 0
        c1 = 0
        c2 = 0
        for v in cycles:
            rem = (v - 1) // 3
            out += rem
            v -= rem * 3
            assert 1 <= v <= 3
            if v == 1:
                c1 += 1
            if v == 2:
                c2 += 1
        cut = min(c1, c2)
        out += cut
        c1 -= cut
        c2 -= cut
        assert c1 % 3 == 0
        assert c2 % 3 == 0
        out += 2 * (c1 // 3)
        out += 3 * (c2 // 3)
    if n % 3 == 1:
        out = 0
        c1 = 0
        c2 = 0
        c4 = 0
        for v in cycles:
            rem = max(0, (v - 2) // 3)
            out += rem
            v -= rem * 3
            assert 1 <= v <= 4
            if v == 1:
                c1 += 1
            if v == 2:
                c2 += 1
            if v == 4:
                c4 += 1
        if c4:
            c4 -= 1
            out += c4
            c1 += c4
            c4 += 1

        def solve(c1, c2):
            ret = 0
            if c2 >= 2:
                c2 -= 2
            elif c2 == 1:
                c2 -= 1
                c1 -= 2
                ret += 1
            elif c1 > 1:
                c1 -= 4
                ret += 2
            else:
                c1 -= 1
                ret += 1
            cut = min(c1, c2)
            ret += cut
            c1 -= cut
            c2 -= cut
            assert c1 % 3 == 0
            assert c2 % 3 == 0
            ret += 2 * (c1 // 3)
            ret += 3 * (c2 // 3)
            return ret
        if c4:
            o1 = out + 1 + solve(c1 + 1, c2)
            cut = min(c1, c2)
            out += cut
            c1 -= cut
            c2 -= cut
            assert c1 % 3 == 0
            assert c2 % 3 == 0
            out += 2 * (c1 // 3)
            out += 3 * (c2 // 3)
            out = min(out, o1)
        else:
            out += solve(c1, c2)
    if n % 3 == 2:
        out = 0
        c1 = 0
        c2 = 0
        for v in cycles:
            rem = (v - 1) // 3
            out += rem
            v -= rem * 3
            assert 1 <= v <= 3
            if v == 1:
                c1 += 1
            if v == 2:
                c2 += 1
        if c2:
            c2 -= 1
        else:
            c1 -= 2
            out += 1
        cut = min(c1, c2)
        out += cut
        c1 -= cut
        c2 -= cut
        assert c1 % 3 == 0
        assert c2 % 3 == 0
        out += 2 * (c1 // 3)
        out += 3 * (c2 // 3)
    outL.append(str(ans) + ' ' + str(out))
print('\n'.join(outL))

Solution (Python):
import sys, io, os
t = int(input())
outL = []
MOD = 10 ** 9 + 7
for _ in range(t):
    n = int(input())
    p = list(map(lambda x: int(x) - 1, input().split()))
    if n % 3 == 0:
        ans = pow(3, n // 3, MOD)
    if n % 3 == 1:
        ans = pow(3, (n - 4) // 3, MOD) * 4
    if n % 3 == 2:
        ans = 2 * pow(3, (n - 2) // 3, MOD)
    ans %= MOD
    seen = [False] * n
    cycles = []
    for i in range(n):
        if seen[i]:
            continue
        count = 0
        curr = i
        while not seen[curr]:
            seen[curr] = True
            count += 1
            curr = p[curr]
        cycles.append(count)
    if n % 3 == 0:
        out = 0
        c1 = 0
        c2 = 0
        for v in cycles:
            rem = (v - 1) // 3
            out += rem
            v -= rem * 3
            assert 1 <= v <= 3
            if v == 1:
                c1 += 1
            if v == 2:
                c2 += 1
        cut = min(c1, c2)
        out += cut
        c1 -= cut
        c2 -= cut
        assert c1 % 3 == 0
        assert c2 % 3 == 0
        out += 2 * (c1 // 3)
        out += 3 * (c2 // 3)
    if n % 3 == 1:
        out = 0
        c1 = 0
        c2 = 0
        c4 = 0
        for v in cycles:
            rem = max(0, (v - 2) // 3)
            out += rem
            v -= rem * 3
            assert 1 <= v <= 4
            if v == 1:
                c1 += 1
            if v == 2:
                c2 += 1
            if v == 4:
                c4 += 1
        if c4:
            c4 -= 1
            out += c4
            c1 += c4
            c4 += 1

        def solve(c1, c2):
            ret = 0
            if c2 >= 2:
                c2 -= 2
            elif c2 == 1:
                c2 -= 1
                c1 -= 2
                ret += 1
            elif c1 > 1:
                c1 -= 4
                ret += 2
            else:
                c1 -= 1
                ret += 1
            cut = min(c1, c2)
            ret += cut
            c1 -= cut
            c2 -= cut
            assert c1 % 3 == 0
            assert c2 % 3 == 0
            ret += 2 * (c1 // 3)
            ret += 3 * (c2 // 3)
            return ret
        if c4:
            o1 = out + 1 + solve(c1 + 1, c2)
            cut = min(c1, c2)
            out += cut
            c1 -= cut
            c2 -= cut
            assert c1 % 3 == 0
            assert c2 % 3 == 0
            out += 2 * (c1 // 3)
            out += 3 * (c2 // 3)
            out = min(out, o1)
        else:
            out += solve(c1, c2)
    if n % 3 == 2:
        out = 0
        c1 = 0
        c2 = 0
        for v in cycles:
            rem = (v - 1) // 3
            out += rem
            v -= rem * 3
            assert 1 <= v <= 3
            if v == 1:
                c1 += 1
            if v == 2:
                c2 += 1
        if c2:
            c2 -= 1
        else:
            c1 -= 2
            out += 1
        cut = min(c1, c2)
        out += cut
        c1 -= cut
        c2 -= cut
        assert c1 % 3 == 0
        assert c2 % 3 == 0
        out += 2 * (c1 // 3)
        out += 3 * (c2 // 3)
    outL.append(str(ans) + ' ' + str(out))
print('\n'.join(outL))

Solution (Python):
import sys, io, os
t = int(input())
outL = []
MOD = 10 ** 9 + 7
for _ in range(t):
    n = int(input())
    p = list(map(lambda x: int(x) - 1, input().split()))
    if n % 3 == 0:
        ans = pow(3, n // 3, MOD)
    if n % 3 == 1:
        ans = pow(3, (n - 4) // 3, MOD) * 4
    if n % 3 == 2:
        ans = 2 * pow(3, (n - 2) // 3, MOD)
    ans %= MOD
    seen = [False] * n
    cycles = []
    for i in range(n):
        if seen[i]:
            continue
        count = 0
        curr = i
        while not seen[curr]:
            seen[curr] = True
            count += 1
            curr = p[curr]
        cycles.append(count)
    if n % 3 == 0:
        out = 0
        c1 = 0
        c2 = 0
        for v in cycles:
            rem = (v - 1) // 3
            out += rem
            v -= rem * 3
            assert 1 <= v <= 3
            if v == 1:
                c1 += 1
            if v == 2:
                c2 += 1
        cut = min(c1, c2)
        out += cut
        c1 -= cut
        c2 -= cut
        assert c1 % 3 == 0
        assert c2 % 3 == 0
        out += 2 * (c1 // 3)
        out += 3 * (c2 // 3)
    if n % 3 == 1:
        out = 0
        c1 = 0
        c2 = 0
        c4 = 0
        for v in cycles:
            rem = max(0, (v - 2) // 3)
            out += rem
            v -= rem * 3
            assert 1 <= v <= 4
            if v == 1:
                c1 += 1
            if v == 2:
                c2 += 1
            if v == 4:
                c4 += 1
        if c4:
            c4 -= 1
            out += c4
            c1 += c4
            c4 += 1

        def solve(c1, c2):
            ret = 0
            if c2 >= 2:
                c2 -= 2
            elif c2 == 1:
                c2 -= 1
                c1 -= 2
                ret += 1
            elif c1 > 1:
                c1 -= 4
                ret += 2
            else:
                c1 -= 1
                ret += 1
            cut = min(c1, c2)
            ret += cut
            c1 -= cut
            c2 -= cut
            assert c1 % 3 == 0
            assert c2 % 3 == 0
            ret += 2 * (c1 // 3)
            ret += 3 * (c2 // 3)
            return ret
        if c4:
            o1 = out + 1 + solve(c1 + 1, c2)
            cut = min(c1, c2)
            out += cut
            c1 -= cut
            c2 -= cut
            assert c1 % 3 == 0
            assert c2 % 3 == 0
            out += 2 * (c1 // 3)
            out += 3 * (c2 // 3)
            out = min(out, o1)
        else:
            out += solve(c1, c2)
    if n % 3 == 2:
        out = 0
        c1 = 0
        c2 = 0
        for v in cycles:
            rem = (v - 1) // 3
            out += rem
            v -= rem * 3
            assert 1 <= v <= 3
            if v == 1:
                c1 += 1
            if v == 2:
                c2 += 1
        if c2:
            c2 -= 1
        else:
            c1 -= 2
            out += 1
        cut = min(c1, c2)
        out += cut
        c1 -= cut
        c2 -= cut
        assert c1 % 3 == 0
        assert c2 % 3 == 0
        out += 2 * (c1 // 3)
        out += 3 * (c2 // 3)
    outL.append(str(ans) + ' ' + str(out))
print('\n'.join(outL))