我们知道,当循环长度为L时,置换群幂次为K ,则结果是GCD(L,K)个积相乘。

于是,我们只需要求出每个循环的长度,求得它们的最小公倍数即为解。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring> #define LL __int64
#define N 1000
using namespace std; int stack[N+1],top;
int num[N+1];
bool vis[N+1]; LL gcd(LL a,LL b){
if(b==0) return a;
return gcd(b,a%b);
} int main(){
int n,tmp;
while(scanf("%d",&n)!=EOF){
top=0;
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;i++){
tmp=0;
if(!vis[i]){
while(!vis[i]){
vis[i]=true;
tmp++;
i=num[i];
}
stack[++top]=tmp;
}
}
LL ans=(LL)stack[top];
for(int i=top-1;i>0;i--){
ans=ans*(LL)stack[i]/gcd(ans,(LL)stack[i]);
}
printf("%I64d\n",ans);
}
return 0;
}

  

POJ 2369的更多相关文章

  1. poj 2369 Permutations - 数论

    We remind that the permutation of some final set is a one-to-one mapping of the set onto itself. Les ...

  2. POJ 2369 Permutations

    傻逼图论. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...

  3. poj 2369 Permutations 置换

    题目链接 给一个数列, 求这个数列置换成1, 2, 3....n需要多少次. 就是里面所有小的置换的长度的lcm. #include <iostream> #include <vec ...

  4. poj 2369 Permutations 更换水称号

    寻找循环节求lcm够了,如果答案是12345应该输出1.这是下一个洞. #include<iostream> #include<cstdio> #include<cstr ...

  5. poj 2369 Permutations (置换入门)

    题意:给你一堆无序的数列p,求k,使得p^k=p 思路:利用置换的性质,先找出所有的循环,然后循环中元素的个数的lcm就是答案 代码: #include <cstdio> #include ...

  6. POJ 2369 Permutations(置换群概念题)

    Description We remind that the permutation of some final set is a one-to-one mapping of the set onto ...

  7. poj 2369(置换群)

    Permutations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3041   Accepted: 1641 Desc ...

  8. POJ 2369 Permutations (置换的秩P^k = I)

    题意 给定一个置换形式如,问经过几次置换可以变为恒等置换 思路 就是求k使得Pk = I. 我们知道一个置换可以表示为几个轮换的乘积,那么k就是所有轮换长度的最小公倍数. 把一个置换转换成轮换的方法也 ...

  9. acm数学(待续)

    意图写出http://www.cnblogs.com/kuangbin/archive/2012/08/28/2661066.html这个东西的完善版. 1.置换,置换的运算 poj 2369 Per ...

随机推荐

  1. CF802G Fake News (easy)

    CF802G Fake News (easy) 题意翻译 给定一个字符串询问能否听过删除一些字母使其变为“heidi” 如果可以输出“YES”,不然为“NO” 题目描述 As it's the fir ...

  2. POJ 2407

    裸 的求欧拉函数 #include <iostream> #include <cstdio> #include <cstring> #include <alg ...

  3. Rails中关联数据表的添加操作(嵌套表单)

    很早就听说有Web敏捷开发这回事,最近终于闲了下来,可以利用业余的时间学些新东西,入眼的第一个东东自然是Ruby on Rails.Rails中的核心要素也就是MVC.ORM这些了,因此关于Rails ...

  4. idea使用技巧资料篇

    基本使用:https://my.oschina.net/lujianing/blog/177042#OSC_h1_2 解决idea卡顿问题:http://www.cnblogs.com/ae6623/ ...

  5. h5-注册成功

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdUAAAGnCAIAAABuMVpqAAAgAElEQVR4nOy9eXQTd57om2R6uvtO3z ...

  6. 蓝桥杯-- 历届试题 核桃的数量 (gcd)

      历届试题 核桃的数量   时间限制:1.0s   内存限制:256.0MB        问题描述 小张是软件项目经理,他带领3个开发组.工期紧,今天都在加班呢.为鼓舞士气,小张打算给每个组发一袋 ...

  7. WCF:目录

    ylbtech-WCF:目录 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://ylbtech.c ...

  8. BigInteger类型转换成Long类型或int类型问题

            BigInteger bi = new BigInteger("123");           int i = bi.intValue();         lo ...

  9. [Tomcat]Tomcat6和Tomcat7的区别

    Tomcat 7最大的改进是对Servlet 3.0和Java EE 6的支持.◆Tomcat 7完全支持Servlet 3.0规范◆Tomcat 7新增了对Java注释的支持◆Tomcat 7通过w ...

  10. ffmpeg x264编译与使用介绍

    问题1:我用的是最新版本的ffmpeg和x264,刚刚编译出来,编译没有问题,但是在linux 环境使用ffmpeg的库时发现报错error C3861: 'UINT64_C': identifier ...