题意

给定一个置换形式如,问经过几次置换可以变为恒等置换

思路

就是求k使得Pk = I.

我们知道一个置换可以表示为几个轮换的乘积,那么k就是所有轮换长度的最小公倍数.

把一个置换转换成轮换的方法也很简单,从一个数出发按照置换图置换,直到置换到已经置换过的数,则这些数就构成一个轮换。

代码

[cpp]
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#define MID(x,y) ((x+y)/2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i, begin, end) for (int i = begin; i <= end; i ++)
using namespace std;

int gcd(int a, int b){
return b?gcd(b, a%b):a;
}
int a[1005];
bool vis[1005];
int main(){
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
int n;
while(scanf("%d", &n) != EOF){
for (int i = 1; i <= n; i ++){
scanf("%d", &a[i]);
}
MEM(vis, false);
int res = 1;
for (int i = 1; i <= n; i ++){
if (!vis[i]){
vis[i] = 1;
int len = 1;
int p = i;
while (1){
p = a[p];
if (vis[p]) break;
vis[p] = 1;
len ++;
}
res = res / gcd(res, len) * len;
}
}
printf("%d\n", res);
}
return 0;
}
[/cpp]

POJ 2369 Permutations (置换的秩P^k = I)的更多相关文章

  1. poj 2369 Permutations 置换

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

  2. poj 2369 Permutations (置换入门)

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

  3. poj 2369 Permutations - 数论

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

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

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

  5. POJ 2369 Permutations

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

  6. poj 2369 Permutations 更换水称号

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

  7. poj3270 && poj 1026(置换问题)

    | 1 2 3 4 5 6 | | 3 6 5 1 4 2 | 在一个置换下,x1->x2,x2->x3,...,xn->x1, 每一个置换都可以唯一的分解为若干个不交的循环 如上面 ...

  8. 【UVA 11077】 Find the Permutations (置换+第一类斯特林数)

    Find the Permutations Sorting is one of the most used operations in real life, where Computer Scienc ...

  9. UVA - 11077 Find the Permutations (置换)

    Sorting is one of the most usedoperations in real life, where Computer Science comes into act. It is ...

随机推荐

  1. php 通过http user-agent判断是否为手机浏览器

    <?php/*** 判断是否是通过手机访问* @return bool 是否是移动设备    */public function isMobile() {  //判断手机发送的客户端标志  if ...

  2. 127. Word Ladder(单词变换 广度优先)

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  3. 32. Longest Valid Parentheses(最长括号匹配,hard)

      Given a string containing just the characters '(' and ')', find the length of the longest valid (w ...

  4. GYM - 101490 J Programming Tutors (匈牙利+二分)

    题意:有N个学生和N个老师,每个人都有自己的坐标X,Y,给每个学生匹配一个老师,要求N个匹配中的距离最大值最小.其中距离的定义为:|X − X’| + |Y − Y ‘|. 分析:一道典型的最大值最小 ...

  5. Jquery Easy UI Datagrid 上下移动批量保存数据

    DataGrid with 上下移动批量保存数据 通过前端变量保存修改数据集合,一次性提交后台执行 本想结合easyui 自带的$('#dg').datagrid('getChanges'); 方法来 ...

  6. Druid学习之路 (四)Druid的数据采集格式

    作者:Syn良子 出处:https://www.cnblogs.com/cssdongl/p/9715735.html 转载请注明出处 Druid的数据采集格式 Druid可以采集非标准化的数据诸如J ...

  7. 查看ubuntu 各系统的内核版本

    1.查看ubuntu版本号:   cat  /etc/issue 返回结果: Ubuntu 16.04.2 LTS \n \l   2.查看内核版本号:   cat /proc/version 返回结 ...

  8. hive union all使用注意

    UNION用于联合多个select语句的结果集,合并为一个独立的结果集,结果集去重. UNION ALL也是用于联合多个select语句的结果集.但是不能消除重复行.现在hive只支持UNION AL ...

  9. 照着官网来安装openstack pike之neutron安装

    neutron组件安装分为控制节点和计算节点,还是先从控制节点安装 1.前提条件,数据库为nova创建库和账户密码来连接数据库 # mysql -u root -p MariaDB [(none)]& ...

  10. 20145303刘俊谦 《Java程序设计》第4周学习总结

    20145303刘俊谦 <Java程序设计>第4周学习总结 教材学习内容总结 1.继承:继承是面向对象的第二大特征,避免多个类间重复定义共同行为.把单独抽取出来的那个类称为父类,其他的多个 ...