POJ 2369 Permutations (置换的秩P^k = I)
题意
给定一个置换形式如
,问经过几次置换可以变为恒等置换
思路
就是求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)的更多相关文章
- poj 2369 Permutations 置换
题目链接 给一个数列, 求这个数列置换成1, 2, 3....n需要多少次. 就是里面所有小的置换的长度的lcm. #include <iostream> #include <vec ...
- poj 2369 Permutations (置换入门)
题意:给你一堆无序的数列p,求k,使得p^k=p 思路:利用置换的性质,先找出所有的循环,然后循环中元素的个数的lcm就是答案 代码: #include <cstdio> #include ...
- poj 2369 Permutations - 数论
We remind that the permutation of some final set is a one-to-one mapping of the set onto itself. Les ...
- POJ 2369 Permutations(置换群概念题)
Description We remind that the permutation of some final set is a one-to-one mapping of the set onto ...
- POJ 2369 Permutations
傻逼图论. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...
- poj 2369 Permutations 更换水称号
寻找循环节求lcm够了,如果答案是12345应该输出1.这是下一个洞. #include<iostream> #include<cstdio> #include<cstr ...
- poj3270 && poj 1026(置换问题)
| 1 2 3 4 5 6 | | 3 6 5 1 4 2 | 在一个置换下,x1->x2,x2->x3,...,xn->x1, 每一个置换都可以唯一的分解为若干个不交的循环 如上面 ...
- 【UVA 11077】 Find the Permutations (置换+第一类斯特林数)
Find the Permutations Sorting is one of the most used operations in real life, where Computer Scienc ...
- UVA - 11077 Find the Permutations (置换)
Sorting is one of the most usedoperations in real life, where Computer Science comes into act. It is ...
随机推荐
- Python eval() 的使用:将字符串转换为列表,元祖,字典
eval() 函数用来执行一个字符串表达式,并返回表达式的值. 语法 以下是 eval() 方法的语法: eval(expression[, globals[, locals]]) 参数 expres ...
- sql 服务器链接远程 sql 服务器 脚本
exec sp_droplinkedsrvlogin 'test',null exec sp_dropserver 'test' exec sp_addlinkedserver@server='Tes ...
- unity手势插件《FingerGestures 》使用入门
什么是FingerGestures? FingerGestures是Unity上,非常热门的一款用于处理用户输入的插件 为什么要使用FingerGestures? 1:它统一了鼠标点击和用户触摸的输入 ...
- 设置eclipse编码格式
1.修改eclipse默认工作空间编码方式.点击Window-->Preferences-->General-->Workspace,设置编码格式为UTF-8,然后点击OK.
- Ubuntu16.04安装和卸载MySQL 5.7
介绍: MySQL 是一种开源数据库管理系统,通常作为流行的LAMP(Linux,Apache,MySQL,PHP / Python / Perl)堆栈的一部分安装.它使用关系数据库和SQL(结构化查 ...
- node异步流程控制async
1.串行无关联:async.series(tasks,callback); 多个函数依次执行,之间没有数据交换,其中一个函数出错,后续函数不再执行 async.series({ one: functi ...
- centos6.9 升级glibc(升级到 2.17版)
原系统centos6.9自带GLIBC_2.12,安装一些软体提示版本不对,决定升级. wget http://ftp.gnu.org/gnu/glibc/glibc-2.17.tar.gz tar ...
- Django学习笔记之Django的url反向解析
0x00 URL反向解析和三种不同的反向解析方式 Django中提供了关于URL的映射的解决方案,可以做两个方向的使用: 1.普通解析过程:由客户端的浏览器发起一个url请求,Django根据URL解 ...
- MVC的局部视图传参的小技巧--见人才网头部导航
当我们设计一个局部视图时,当出现有类似导航的功能(如:选择左边的某个按钮跳到某个页,且顶部导航也作相印改变),如果我们选择把导航作为局部视图来处理,调用就可以做如下处理: @Html.RenderAc ...
- 20145216史婧瑶《Java程序设计》第三次实验报告
实验三 敏捷开发与XP实践 实验内容 使用git上传代码,两个人进行小组合作,队友下载代码并修改再重新上传. 实验步骤 一. 使用git上传代码 1.找到需要push的文件所在文件夹,右键点击Git ...