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. go递归打印指定目录下的所有文件及文件夹

    func treedir(fpath string){ // 获取fileinfo if finfo,err := os.Stat(fpath); err == nil { // 判断是不是目录 如果 ...

  2. 使用spring cache和ehcache

    一.spring cache Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该方法参数和返回结果作为一个键值对存放在缓存中,等到下次利用同样的参数来调用该 ...

  3. 利用ansible来做tomcat应用的持续交付

    https://www.jianshu.com/p/fca8f91ae223 在做持续交付这件事,想必大家都是用jenkins这款程序来做基石.当然,我们这次也是用jenkins作为承载工具,jenk ...

  4. PHP_EOL 写入字符串换行 , php获取毫秒 microtime

    private function miclog($t1,$t2,$name){ $lasttime = ($t2 - $t1).'ms'; $content = date('Y-m-d H:i:s', ...

  5. NATS_03:NATS发布/订阅机制

    概念 发布/订阅(Publish/subscribe 或pub/sub)是一种消息范式,消息的发送者(发布者)不是计划发送其消息给特定的接收者(订阅者).而是发布的消息分为不同的类别,而不需要知道什么 ...

  6. Oracle 中常用数据字典大总结

    原文出处:小宝马的爸爸 - 梦想的家园 前面呢,也断断续续的介绍了一些诸如 Sql*Plus 等等关于 Oracle 的基本的内容, 对于 Oracle 这样的大型数据库呢,自身的运行和维护也是个不得 ...

  7. SQL优化:

    今日给人查找数据,时间关系,写个比较粗暴的SQL语句:   #2s587ms#直接将所有表关联,比较粗暴 select go.businessId,dd.dict_namefrom fn_xte.gt ...

  8. 递归和静态static

    function sum($n){ if($==1){ return 1; } return $n+sum($n-1); } echo sum(100); ---------------------- ...

  9. github 新创建repositories

    1. 在github上新建repo 2. 找到改repo的地址,用命令git clone https://....,  拉取到本地 3. 打开一个单独的窗口,打开此文件夹 4. 创建自己的python ...

  10. Postgresql数据库安装中文全文搜索插件zhparser的问题

    在PG数据库的基础上加装zhparser中文全文搜索插件,说实话,挺怕这些单独编译安装的插件的,因为安装PG数据库方法的不同,最后可能导致安装的插件各种安装不上,这里说一下我遇到的坑,系统环境是Cen ...