简单题
View Code
#include#include #include #include using namespace std; #define maxn 1005 int f[maxn]; int n; bool vis[maxn]; int gcd(int x, int y) { if (!x || !y) return x > y ? x : y; for (int t; t = x %y; x = y, y = t); return y; } int main() { //freopen("t.txt", "r", stdin); scanf("%d", &n); for (int i = 0; i< n; i++) { int a; scanf("%d", &a); a--; f[a] = i; } memset(vis, 0, sizeof(vis)); int ans = 1; for (int i = 0; i < n; i++) if (!vis[i]) { int a = i; int b = 0; while (!vis[a]) { vis[a] = true; a = f[a]; b++; } ans = ans / gcd(ans, b) * b; } printf("%d\n", ans); return 0; }