2020牛客暑期多校训练营(第五场)E.Bogo Sort

2020牛客暑期多校训练营(第五场)E.Bogo Sort

题目链接

题目描述

Today Tonnnny the monkey learned a new algorithm called Bogo Sort. The teacher gave Tonnnny the code of Bogo sort:

bool is_sorted(int a[], int n) {
    for (int i = 1; i < n; i++) {
        if (a[i] < a[i - 1]) {
            return false;
        }
    }
    return true;
}
void bogo_sort(int a[], int n) {
    while (!is_sorted(a, n)) {
        shuffle(a, a + n);
    }
} 

The teacher said the shuffle function is to uniformly randomly permute the array with length , and the algorithm’s expectation complexity is O ( n ⋅ n ! ) O(n \cdot n!) O(nn!).
However, Tonnnny is a determined boy — he doesn’t like randomness at all! So Tonnnny improved Bogo Sort. He had chosen one favourite permutation with length , and he replaced the random shuffle with shuffle of , so the improved algorithm, Tonnnny Sort, can solve sorting problems for length array — at least Tonnnny thinks so.

int p[N] = {....}; // Tonnnny's favorite permutation of n
void shuffle(int a[], int n) {
    int b[n];
    for (int i = 0; i < n; i++) {
        b[i] = a[i]
    }
    for (int i = 0; i < n; i++) {
        a[i] = b[p[i]];
    }
}
void tonnnny_sort(int a[], int n) {
    assert (n == N); // Tonnnny appointed!
    while (!is_sorted(a, n)) {
        shuffle(a, a + n);
    }
}

Tonnnny was satsified with the new algorithm, and decided to let you give him a different array of length every day to sort it with Tonnnny Sort.

You are the best friend of Tonnnny. Even though you had found the algorithm is somehow wrong, you want to make Tonnnny happy as long as possible. You’re given N, p, and you need to calculate the maximum number of days that Tonnnny will be happy, since after that you can’t give Tonnnny an array that can be sorted with Tonnnny Sort and didn’t appeared before.

The answer may be very large. Tonnnny only like numbers with at most digits, so please output answer mod 1 0 N 10^N 10N instead.

输入描述:

The first line contains one integer N ( 1 ≤ N ≤ 1 0 5 ) N (1 \leq N \leq 10^5) N(1N105)
The second line contains integer indicating , which forms a permutation of 1 , 2 , ⋯   , N 1, 2, \cdots, N 1,2,,N.

输出描述:

The maximum number of days that Tonnnny will be happy, module 1 0 N 10 ^ N 10N .

示例1

输入

5
1 2 3 4 5

输出

1

示例2

输入

6
2 3 4 5 6 1

输出

6

遇事不决先打表,打表后直接 oeis
发现答案和 p p p 的排列有关,就是所有循环节的最小公倍数,取模其实没有卵用,然后 C 试水,WA吐了,我天真的以为题目的数据会在 l o n g l o n g long long longlong 以内/(ㄒoㄒ)/~~,改成 py 一发过了,所以下次做题别天真了,大数果断上 py 或者 java,AC代码如下:

import math
def dfs(i):
    global vis
    u=i
    ans=1
    while (p[i]-1)!=u:
        ans+=1
        vis[i]=1
        i=p[i]-1
    return ans
vis=[0]*100005
n,*p=map(int,open(0).read().split())
ans=1
for i in range(n):
    if vis[i]:
        continue
    k=dfs(i)
    ans=ans*k//math.gcd(ans,k)
print(ans)

你可能感兴趣的:(高精度,牛客,python)