[poj2369]Permutations
来源:互联网 发布:怎么抢淘宝秒杀的东西 编辑:程序博客网 时间:2024/06/05 23:45
一句话题意:求一个排列的所有循环的最小公倍数。
正好前几天因为牛排序而知道了这个东西,做一做模板题练一练手。
其主要思想就是两个数组互相迭代,可以用简单的例子来模拟一下也是可以的。
#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<string>#include<cstring>#include<iostream>#define N 1000using namespace std;int n,B[N+5],A[N+5],tmp;bool vis[N+5];long long cnt,ans;long long gcd(long long a,long long b){ return b==0?a:gcd(b,a%b);}int main(){ scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&A[i]); for(int i=1;i<=n;i++)B[A[i]]=i; sort(A+1,A+n+1);ans=1; while(true) { int i; for(i=1;i<=n;i++)if(!vis[A[i]])break; if(i==n+1)break; tmp=A[i]; cnt=0; while(true) { vis[tmp]=true;cnt++; if(vis[A[B[tmp]]])break; tmp=A[B[tmp]]; } ans=ans*cnt/gcd(ans,cnt); } cout<<ans; return 0;}
阅读全文
0 0
- [poj2369]Permutations
- 【置换群】 poj2369 Permutations
- poj2369 Permutations 置换群
- POJ2369 Permutations【置换群】
- POJ2369 Permutations 置换群循环节
- POJ2369
- POJ2369————Permutations(置换群,数论)
- POJ2369 置换群
- poj2369 置换群基础
- POJ2369【循环节】
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Python算法之快排学习总结
- matlab中连通域处理函数
- 计算机网络作业
- HDOJ 1019 水题
- Centos7 手把手教你 编译安装LNMP环境
- [poj2369]Permutations
- 面试题
- HAProxy
- 使用python的os.walk()包来监测文件变化
- 2017秋招之面试整理(2)
- String和StringBuilder、StringBuffer的区别?
- JAVA面试掌握的技术点
- 面试题
- 关于二叉树的一些简单理解