《POJ-2369》
求置换群的阶数:https://www.cnblogs.com/zwjzwj/p/14819849.html
注意的是ans相乘后可能爆int。
// Author: levil #include<iostream> #include<stdio.h> #include<queue> #include<algorithm> #include<math.h> #include<stack> #include<map> #include<limits.h> #include<vector> #include<string.h> #include<string> using namespace std; using namespace std; typedef long long LL; typedef pair<LL,int> pii; const int N = 1e3 + 5; const int M = 1e5 + 5; const LL Mod = 199999; #define pi acos(-1) #define INF 1e9 #define dbg(ax) cout << "now this num is " << ax << endl; namespace FASTIO{ inline LL read(){ LL x = 0,f = 1;char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();} while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();} return x*f; } void print(int x){ if(x < 0){x = -x;putchar('-');} if(x > 9) print(x/10); putchar(x%10+'0'); } } using namespace FASTIO; int n,a[N],p[N],pp[N],cnt[N],tot = 0; bool solve() { for(int i = 1;i <= n;++i) { pp[i] = a[p[i]]; } for(int i = 1;i <= n;++i) { if(pp[i] == i && cnt[i] == 0) cnt[i] = tot; p[i] = pp[i]; } for(int i = 1;i <= n;++i) if(cnt[i] == 0) return false; return true; } int main() { while(cin >> n) { memset(cnt,0,sizeof(cnt)); for(int i = 1;i <= n;++i) a[i] = read(),p[i] = a[i]; LL ans = 1; tot = 1; for(int i = 1;i <= n;++i) if(p[i] == i) cnt[i] = 1; while(1) { ++tot; if(solve()) break; } for(int i = 1;i <= n;++i) if(cnt[i] != 0) ans = ans * cnt[i] / __gcd(ans,1LL * cnt[i]); printf("%lld\n",ans); } system("pause"); return 0; }