题解:
显然把每一个环求出来
然后做一个lcm即可
代码:
#include<cstdio> using namespace std; int a[1005],f[1005],n; int gcd(int x,int y) { if (!y)return x; return gcd(y,x%y); } int main() { scanf("%d",&n); for (int i=1;i<=n;i++)scanf("%d",&a[i]); int ans=1; for (int i=1;i<=n;i++) if (!f[i]) { int j=i,num=0; while (j!=i||num==0) { f[j]=1; j=a[j]; num++; } ans=ans/gcd(ans,num)*num; } printf("%d",ans); }