侧边栏

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

链接

https://ac.nowcoder.com/acm/contest/5670/E

题意

给定一个排序,给定这个排序的置换,问置换多少次会回到最初的状态

思路

就是离散里的置换群循环次数,裸题,由于涉及到大数,用了python,但是要注意的是python里的gcd不要用递归式的,会爆栈,python的递归最多只有1000层

代码

import math

nxt = [0]
ok = []
maxn = 100005
# def gcd(a, b):
#     if b == 0:
#         return a
#     else:
#         return gcd(b,a%b)

n = int(input())
mod = pow(10,n)

nxt = nxt + list(map(int,input().split()))

# print(nxt)
for i in range(0,maxn):
    ok.append(False)

ans = 1

for i in range(1,n+1):
    if ok[i] == False:
        tmp = i
        cnt = 1
        ok[i] = True
        while nxt[tmp] != i:
            cnt = cnt + 1
            tmp = nxt[tmp]
            ok[tmp] = True
        css = math.gcd(ans,cnt)
        ans = (ans * cnt) // css
        
print(ans)

posted @ 2020-07-26 17:51  晴人  阅读(205)  评论(0编辑  收藏  举报