poj 2369 Permutations(置換群)

來源:互聯網
上載者:User
早起一水.
求原始序列到有序序列按照規則最少需要移動多少次.
分析下範例
1 2 3 4 5 

原始序列: 4 1 5 2 3

2 4 3 1 5
p(p(1))=p(4)=2;
p(p(2))=p(1)=4;
p(p(3))=p(5)=3;
...
...

1 2 5 4 3
p(p(p(1)))=p(2)=1;
p(p(p(2)))=p(4)=2;
p(p(p(3)))=p(3)=5;
...
...4 1 3 2 5
p(p(p(p(1))))=p(1)=4;
p(p(p(p(2))))=p(2)=1;
p(p(p(p(3))))=p(5)=3;
2 4 5 1 31 2 3 4 5

就是兩個迴圈節(1,2,4) 和(3,5) 我們只要求出lcm(迴圈節長度)便是其需要移動的次數


http://blog.csdn.net/kksleric/article/details/7793397這篇文章或許可以看看

.

/*Problem ID: poj 2369meaning: 求亂序到有序最少交換次數Analyzing: 置換群*/#include <iostream>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<vector>using namespace std;typedef struct even{int y1,y2,x;}even;#define FOR(i,s,t) for(int i=(s); i<(t); i++)#define LL long long#define BUG puts("here!!!")#define STOP system("pause")#define file_r(x) freopen(x, "r", stdin)#define file_w(x) freopen(x, "w", stdout)#define maxn 1006LL gcd(LL a,LL b) {return a?gcd(b%a,a):b;}int main(){    int N;    int A[maxn],vis[maxn];    scanf("%d",&N);    FOR(i,0,N){        scanf("%d",&A[i]);        A[i]=A[i]-1;    }    LL res=1,len;    memset(vis,0,sizeof(vis));    FOR(i,0,N){        if(!vis[i]){            int j=i;            len=0;            while(!vis[j]){                len++;                vis[j]=1;                j=A[j];            }        }        res=res/gcd(res,len)*len;    }    printf("%d\n",res);    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.