Permutations
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3041   Accepted: 1641

Description

We remind that the permutation of some final set is a one-to-one mapping of the set onto itself. Less formally, that is a way to reorder elements of the set. For example, one can define a permutation of the set {1,2,3,4,5} as follows:


This record defines a permutation P as follows: P(1) = 4, P(2) = 1, P(3) = 5, etc.

What is the value of the expression P(P(1))? It’s clear, that
P(P(1)) = P(4) = 2. And P(P(3)) = P(5) = 3. One can easily see that if
P(n) is a permutation then P(P(n)) is a permutation as well. In our
example (believe us)



It is natural to denote this permutation by P2(n) = P(P(n)). In a
general form the defenition is as follows: P(n) = P1(n), Pk(n) =
P(Pk-1(n)). Among the permutations there is a very important one — that
moves nothing:



It is clear that for every k the following relation is satisfied:
(EN)k = EN. The following less trivial statement is correct (we won't
prove it here, you may prove it yourself incidentally): Let P(n) be some
permutation of an N elements set. Then there exists a natural number k,
that Pk = EN. The least natural k such that Pk = EN is called an order
of the permutation P.

The problem that your program should solve is formulated now in a very simple manner: "Given a permutation find its order."

Input

In
the first line of the standard input an only natural number N (1 <= N
<= 1000) is contained, that is a number of elements in the set that
is rearranged by this permutation. In the second line there are N
natural numbers of the range from 1 up to N, separated by a space, that
define a permutation — the numbers P(1), P(2),…, P(N).

Output

You
should write an only natural number to the standard output, that is an
order of the permutation. You may consider that an answer shouldn't
exceed 109.

Sample Input

5
4 1 5 2 3

Sample Output

6

题意:给出一个集合 p,每次按照一个规则去换掉其中的元素,问使 p^k = p 成立的最小的 k 是多少?

题解:我们将该集合的所有置换群求出来,那么最小的k就是这些群的循环的最小公倍数,比如:
1 2 3 4 5
4 1 5 2 3
上面可以划分出两个群 (1,4,2),(3,5)这两个群的循环分别是 3 ,2 所以最小的 k 就是 3*2 = 6
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
typedef long long LL;
const int N = ;
int n;
struct Node{
int val,id;
}node[N];
bool vis[N];
int cmp(Node a,Node b){
return a.val < b.val;
}
int gcd(int a,int b){
return b==?a:gcd(b,a%b);
}
int main()
{
while(scanf("%d",&n)!=EOF){
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++){
scanf("%d",&node[i].val);
node[i].id = i;
}
sort(node+,node++n,cmp);
int lcm = ;
for(int i=;i<=n;i++){
int loop = ;
int t = i;
while(!vis[t]){
loop++;
vis[t] = true;
t = node[t].id;
}
if(loop){
lcm = lcm/gcd(lcm,loop)*loop;
}
}
printf("%d\n",lcm);
}
return ;
}

poj 2369(置换群)的更多相关文章

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

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

  2. poj 2369 Permutations - 数论

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

  3. poj 1026(置换群)

    题意:给你一个变换规则,和一个字符串,问经过k次变换后得到的字符串. 思路:开始的时候试图去找它的整个周期,谁知道周期太大了,各种RE,后来在得知此题需要用置换群来优化,第一次接触置换群学习了下! 代 ...

  4. poj 3270(置换群+贪心)

    Cow Sorting Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6993   Accepted: 2754 Descr ...

  5. POJ 3270 置换群问题

    题目大意是: 每头牛都有一个对应的值a[i],现在给定一个初始的牛的序列,希望通过两两交换,能够使这些牛按值升序排列,每次交换都会耗费一个 a[i]+a[j] 希望耗费最小,求出这个最小耗费 个人觉得 ...

  6. POJ 1026 置换群的k次幂问题

    题目大意: 给定了一组对应关系,经过k次幂后,得到新的对应关系b[i],然后将给定的字符串上的第i位字符放置到b[i]的位置上, 如果字符串长度不足n就用空格补足,这里的是空格,也就是str[i] = ...

  7. POJ 2369

    我们知道,当循环长度为L时,置换群幂次为K ,则结果是GCD(L,K)个积相乘. 于是,我们只需要求出每个循环的长度,求得它们的最小公倍数即为解. #include <iostream> ...

  8. POJ 2369 Permutations

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

  9. poj 3270(置换群)

    题意:给定n头母牛的脾气大小,然后让你通过交换任意两头母牛的位置使得最后的母牛序列的脾气值从小到大,交换两头母牛的代价是两个脾气之和,使得代价最小. 分析:以前做过一道题,只有一个地方和这道题不同,但 ...

随机推荐

  1. Betsy Ross Problem

    Matlab学习中的betsy ross 问题.用matlab函数画1777年的美国国旗. 五角星绘制部分是自己想出来的方法去画上的.具体代码参考如下. 先是绘制矩形的函数 function Draw ...

  2. Selenium--使用参考

    Selenium--浏览器上的按键精灵 Selenium是一个Web的自动化测试工具,最初是为网站自动化测试而开发的,类型像我们玩游戏用的按键精灵,可以按指定的命令自动操作,不同是Selenium 可 ...

  3. css--display属性中inline-block与inline的区别

    inline-block 与 inline 的区别: inline-block 与inline 效果类似,但是inline-block是可以设定宽度和高度的!!而行内元素(也就是inline)是无法设 ...

  4. linux网络编程框架

    OSI七层模型与TCP四层模型 OSI七层模型与TCP四层模型 BS和CS服务器架构 (1)CS架构介绍(client server,客户端服务器架构)(2)BS架构介绍(broswer server ...

  5. windows10下基于vs2015的 caffe安装教程及python接口实现

    啦啦啦:根据网上的教程前一天安装失败,第二天才安装成功.其实caffe的安装并不难,只是网上的教程不是很全面,自己写一个,留作纪念. 准备工作 Windows10 操作系统 vs2015(c++编译器 ...

  6. 简便开发插件 -- lombok

    lombok是一款可以精减java代码.提升开发人员生产效率的辅助工具,可以利用注解在编译期自动生成setter/getter/toString()/constructor代码. 彻底将开发人员从繁琐 ...

  7. Shell记录-Shell脚本基础(一)

    Shell 注释: 你可以把注释,在你的脚本如下: #!/bin/bash # Author : Zara Ali # Copyright (c) Tutorialsyiibai.com # Scri ...

  8. linq的语法和案例

    本篇逐一介绍linq各个关键字的用法(from,select,group,into等),本篇所有的案例都是用linqpad来完成的(官方地址:http://www.linqpad.net/),建议想学 ...

  9. 深入分析tcp close与shutdown

    关闭socket-close 我们知道,tcp是一种支持全双工(full-duplex)通信的的协议,也就是说建立连接的两端可以在同一个时刻发送.接受数据.在需要关闭套接字的时候,我们一般调用: in ...

  10. Java并发编程原理与实战十四:Lock接口的认识和使用

    保证线程安全演进: synchronized volatile AtomicInteger Lock接口提供的方法: void lock():加锁 void unlock():解锁 void lock ...