poj 2369 Permutations
来源:互联网 发布:软件购销合同书范本 编辑:程序博客网 时间:2024/06/09 22:49
题目链接:http://poj.org/problem?id=2369View Code
题意:给出一个序列,问该序列重复几次后可以回到12345... 的情况。
例如,原序列为4 1 5 2 3,重复第二次为2 4 3 1 5,重复第三次为1 2 5 4 3,第四次为4 1 3 2 5 ,第五次为2 4 5 1 3,第六次为1 2 3 4 5。
所以答案为6.
分析:求出每个数变化的周期,而所有周期的最小公倍数就是答案。(最终序列可看做第0次序列,也就是周期中的第一个序列,变换一次就是原序列,所以把总周期求出来就是答案。)
代码:
#include<cstdio>#include<cmath>#include<cstring>#include<queue>#include<stack>#include<cstdlib>#include<string>#include<vector>#include<map>#include<string>#include<iostream>#include<algorithm>using namespace std;#define INF 0x3f3f3f3ftypedef long long ll;#define Max(a,b) (a>b)?a:b#define lowbit(x) x&(-x)int main(){ int n; scanf("%d",&n); int a[1005],p[1005]={0},s[1005]={0},*p1,*p2; ll sum=1,flag=1,t,x; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } for(int i=1;i<=n;i++) { t=0; x=a[i]; while(s[x]==0) { s[x]=1; t++; x=a[x]; } if(t) sum=sum*t/__gcd(sum,t); } printf("%lld\n",sum);}
0 0
- POJ 2369 Permutations
- POJ 2369 Permutations(置换)
- POJ 2369Permutations
- poj 2369 Permutations
- poj 2369 Permutations(置换群)
- poj 2369 Permutations (置换群)
- POJ 2369 - Permutations 【置换群】
- POJ 2369 Permutations (置换群)
- POJ 2369 Permutations 置换群
- poj 2369 Permutations 【置换群】
- poj 2369 Permutations(置换)
- POJ 2369 Permutations (置换群)
- POJ 2369 Permutations 【置换群】
- POJ 2369 Permutations【置换群】
- poj 2369-Permutations置换及其应用
- Cow Sorting && Permutations(poj 3270 && 2369)
- poj 2369 Permutations 置换水题
- 【POJ 2369】Permutations(置换群)
- Hive分析窗口函数(五) GROUPING SETS,GROUPING__ID,CUBE,ROLLUP
- poj 2366 Sacrament of the sum
- poj 2367 Genealogical tree
- Apple Mach-O Linker (ld) Error Group
- poj 2368 Buttons
- poj 2369 Permutations
- poj 2370 Democracy in danger
- jmeter通过jmeter-plugins、jconsole对系统资源进行监控
- poj 2371 Questions and answers
- poj 2372 D++ Again
- HDU 5857 Median (2016 多校训#10 1001)
- HDU 5858 Hard problem (2016 多校训练#10 1002)
- HDU 5867 Water problem (2016 多校训练#10 1011)
- CodeForces 13A Numbers