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 ...
随机推荐
- (二)无状态的web应用(单py的Django占位图片服务器)
本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 阅读本文建议了解Django框架的基本工作流程,了解WSGI应用,如果对以上不是很清楚,建议结 ...
- iis7.5安装配置php环境详细清晰教程,三步实现【图文】
iis7.5安装配置php环境详细清晰教程,三步实现[图文] iis7.5是安装在win7.win8里的web服务器,win2003.win2000的web服务器使用的是iis6.0,由于win7.w ...
- 『NiFi 学习之路』自定义 —— 组件的自定义及使用
一.概述 许多业务仅仅使用官方提供的组件不能够满足性能上的需求,往往要通过高度可定制的组件来完成特定的业务需求. 而 NiFi 提供了自定义组件的这种方式. 二.自定义 Processor 占坑待续 ...
- XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem A. Arithmetic Derivative
题目:Problem A. Arithmetic DerivativeInput file: standard inputOutput file: standard inputTime limit: ...
- [转]loadrunner:系统的平均并发用户数和并发数峰值如何估算
一.经典公式1: 一般来说,利用以下经验公式进行估算系统的平均并发用户数和峰值数据 1)平均并发用户数为 C = nL/T 2)并发用户数峰值 C‘ = C + 3*根号C C是平均并发用户数,n是l ...
- 【转载】IDEA:放置型塔防备忘录
下周开始做原型了,我需要再次细细的整理一遍设计思路,确保每一个设计都能为了我所追求的玩家体验添砖加瓦,而不是互相打架.同时本文还能提供最原始的VISION,待到将来开发万一陷入泥淖,翻出此文来可以起到 ...
- Web服务器文件传输程序客户端程序实现
1. 客户端程序--主函数 客户端主程序的流程图如下: 主程序主要是分析输入的命令,根据不同命令调用不同的函数处理或者进行出错处理,函数代码如下: #include "common.h&qu ...
- JSON.stringify without quotes on properties?
https://stackoverflow.com/questions/11233498/json-stringify-without-quotes-on-properties/11233515 va ...
- 《阿里巴巴Java开发规约》插件使用
通过Jetbrains官方仓库安装 1. 打开 Settings >> Plugins >> Browse repositories... 2. 在搜索框输入alibaba即可 ...
- Vue 组件间传值
前言 Vue 作为现在比较火的框架之一,相信您在使用的过程中,也会遇到组件间传值的情况,本文将讲解几种 Vue 组件间传值的几种方法,跟着小编一起来学习一下吧! 实现 注意: 学习本文,需要您对 Vu ...