首页 > 其他 > 详细

poj 2369 Permutations (置换群)

时间:2014-09-02 15:51:04      阅读:278      评论:0      收藏:0      [点我收藏+]
//问最少置换多少次变成有序序列
//每个位置都有个循环节 求全部位置循环节的最小公倍数
# include <stdio.h>
# include <algorithm>
# include <string.h>
using namespace std;
int gcd(int x,int y)
{
    if(y==0)
        return x;
    return gcd(y,x%y);
}
int lcm(int x,int y)
{
    return x/gcd(x,y)*y;//先除后乘,不会超
}
int main()
{
    int n,i,cot,t;
    int p[1010],p1[1010];
    while(~scanf("%d",&n))
    {
        for(i=1; i<=n; i++)
            scanf("%d",&p[i]);
        for(i=1; i<=n; i++)
        {
            cot=1;
            t=p[i];
            while(t!=i)
            {
                cot++;
                t=p[t];
            }
            p1[i]=cot;

        }
        for(i=2; i<=n; i++) //最小公倍数
        {
            p1[i]=lcm(p1[i],p1[i-1]);
        }
        printf("%d\n",p1[n]);

    }
    return 0;
}

poj 2369 Permutations (置换群)

原文:http://blog.csdn.net/lp_opai/article/details/39007175

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!