ural 1024. Permutations
来源:互联网 发布:数据成熟度模型 编辑:程序博客网 时间:2024/06/06 17:22
一开始直接模拟,果断超时了,后来上网查了查,是组合数学中的置换群。
本题求经过多少次置换P能变成初始状态。
对于任意一个[1,n]的数字,经过多次置换后一定可以变会初始状态,
而且置换的次数不会超过n。
对于第i个数,模拟计算出其置换周期,记为ai,答案就是所有ai的最小公倍数。
1 2 3 4 5
4 1 5 2 3
次数:3 3 2 3 2,最小公倍数6.
1 #include <iostream> 2 #include <cstdio> 3 int gcd(int a, int b) 4 { 5 if (b == 0) 6 return a; 7 else 8 return gcd(b, a % b); 9 }10 int lcm(int a, int b)11 {12 return a / gcd(a, b) * b;13 }14 int rota(int *f, int i)15 {16 int num = 1;17 int t = f[i];18 while (f[i] != f[t])19 {20 t = f[t];21 ++num;22 }23 return num;24 }25 26 int main()27 {28 int n;29 scanf("%d", &n);30 int f[n+1];31 for (int i = 1; i <= n; ++i)32 {33 scanf("%d", &f[i]);34 }35 int ans = 1;36 for (int i = 1; i <= n; ++i)37 {38 int num = rota(f, i);39 ans = lcm(ans, num);40 }41 printf("%d\n", ans);42 //system("pause");43 return 0;44 }
- ural 1024. Permutations
- URAL 1024. Permutations
- ural 1024. Permutations (置换群)
- ural 1024 Permutations
- 1024. Permutations
- POJ 2369|URAL 1024|Permutations|置换求循环节长度
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- hdu 1010 Tempter of the Bone(dfs+奇偶剪枝)
- hdu 1312 Red and Black(dfs入门)
- ural 1033. Labyrinth(dfs)
- 图的dfs与bfs(深搜广搜)c++实现
- hdu 1209 Clock(水题)
- ural 1024. Permutations
- hdu 1518 Square(dfs)
- bjfu 1143 小蝌蚪安家(bfs入门)
- 并查集入门
- hdu 1026 Ignatius and the Princess I(bfs+priority_queue)
- ural 1910. Titan Ruins: Hidden Entrance(Titan Ruins系列题目)
- C++函数对象count_if用法(转)
- ural 1917. Titan Ruins: Deadly Accuracy(Titan Ruins系列)
- oracle 几个用户的默认密码及修改