poj 2369 Permutations [置换群]

题目:  http://poj.org/problem?id=2369

题意:给定一个序列,问需要最少需要置换多少次才能变为有序序列。

很水的置换群(线代接触过类似知识,貌似近世代数还要学)。。

求出所有的周期,然后算下lcm。

#include <iostream>  
using namespace std;
const int MAXN = 2000;
int gcd(int a, int b){  
    return b == 0 ? a : gcd(b, a%b);  
}  
int lcm(int a, int b){  
    return a / gcd(a, b) * b;  
}  
int a[MAXN];
int main() {
    int n;  
    while(cin >> n) {
        for(int i = 1; i <= n; i++) cin >> a[i];  
        int ans = 1; 
        for(int i = 1; i <= n; i++) {   
            int j = a[i]; int cnt = 1;  
            while(i != j) {  
                cnt++;  
                j = a[j];  
            }  
            ans = lcm(ans, cnt);  
        }  
        cout << ans << endl;  
    }  
    return 0;  
}  

 

 

 

原文地址:https://www.cnblogs.com/cniwoq/p/7241026.html