poj2369 Permutations
题解:求出每个数的循环节,求最小公倍数即可。
//置换群
#include<iostream>
#include<cstdio>
using namespace std;
const int N=1e3+10;
int p[N];int gcd(int a,int b){if(b==0) return a;return gcd(b,a%b);
}int lcm(int a,int b){return a/gcd(a,b)*b;
}int main(){int n;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&p[i]);int ans=1,tmp,cnt; for(int i=1;i<=n;i++){int tmp=p[i];cnt=1;while(tmp!=i){cnt++;tmp=p[tmp];}//printf("%d\n",cnt);ans=lcm(ans,cnt);}printf("%d\n",ans);return 0;
}