POJ2369 Permutations【置换群】
题目链接:
http://poj.org/problem?id=2369
题目大意:
给定一个序列。问最少须要多少次置换才干变为 1、2、…、N 的有序序列。比方说给
定5个数的序列 4 1 5 2 3。表示置换为:
( 1 2 3 4 5 ) ,即 (1 4 2)(3 5)
4 1 5 2 3
解题思路:
对于每一位找到自己轮换内轮换到自己的次数。求不相交的轮换之间的次数的公倍数,
即为终于结果。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std; int GCD(int a,int b)
{
if(b == 0)
return a;
return GCD(b,a%b);
} int LCM(int a,int b)
{
return a / GCD(a,b) * b;
} int A[1100],vis[1100];//vis[]标记轮换 int main()
{
int N;
while(~scanf("%d",&N))
{
memset(vis,0,sizeof(vis));
for(int i = 1; i <= N; ++i)
scanf("%d",&A[i]);
int Ans = 1;
for(int i = 1; i <= N; ++i)
{
int tmp = A[i];
int Num = 1;
vis[i] = 1;
while(tmp != i && !vis[tmp])
{
vis[tmp] = 1;
tmp = A[tmp];
Num++;
}
Ans = LCM(Ans,Num);
}
printf("%d\n",Ans);
} return 0;
}
POJ2369 Permutations【置换群】的更多相关文章
- poj2369 Permutations ——置换群
link:http://poj.org/problem?id=2369 置换群,最简单的那种. 找所有数字循环节的最小公倍数. /* ID: zypz4571 LANG: C++ TASK: perm ...
- POJ2369 Permutations(置换的周期)
链接:http://poj.org/problem?id=2369 Permutations Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 2369 Permutations(置换群概念题)
Description We remind that the permutation of some final set is a one-to-one mapping of the set onto ...
- Uva 11077 Find the Permutations [置换群 DP]
题意: 给定$n$和$k$,问有多少排列交换$k$次能变成升序 $n \le 21$ $uva$貌似挂掉了$vjudge$上一直排队 从某个排列到$1,2,...,n$和从$1,2,...,n$到某个 ...
- POJ置换群入门[3/3]
POJ 3270 Cow Sorting 题意: 一个序列变为升序,操作为交换两个元素,代价为两元素之和,求最小代价 题解: 看了黑书... 首先循环因子分解 一个循环完成的最小代价要么是循环中最小元 ...
- acm数学(待续)
意图写出http://www.cnblogs.com/kuangbin/archive/2012/08/28/2661066.html这个东西的完善版. 1.置换,置换的运算 poj 2369 Per ...
- poj1026 Cipher ——置换群
link:http://poj.org/problem?id=1026 其实这道题目和poj2369这道题目一样. 都是基础的置换群题目.把那道题目理解了,这道题就没问题了. 不过我的方法貌似比较挫, ...
- poj 2369(置换群)
Permutations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3041 Accepted: 1641 Desc ...
- Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- 运维派 企业面试题4&5 创建10个 用户 ; ping探测主机是否在线
Linux运维必会的实战编程笔试题(19题) 企业面试题4: 批量创建10个系统帐号oldboy01-oldboy10并设置密码(密码为随机8位字符串). #!/bin/bash # ;i<=; ...
- css 禁止文本被选中复制代码
css 禁止文本被选中复制代码: .cus-text{ -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none ...
- Hadoop-2.4.1 ubuntu集群安装配置教程
一.环境 系统: Ubuntu 14.04 32bit Hadoop版本: Hadoop 2.4.1 (stable) JDK版本: 1.7 集群数量:3台 注意事项:我们从Apache官方网站下载的 ...
- 【Paper Reading】Improved Textured Networks: Maximizing quality and diversity in Feed-Forward Stylization and Texture Synthesis
Improved Textured Networks: Maximizing quality and diversity in Feed-Forward Stylization and Texture ...
- OO问题
设计一个在线的酒店预订系统,并且可以通过城市搜索出来 解决办法: Main Class: User Room Hotel Booking Adress Enums : 房间的状态和类型 public ...
- MongoDB count distinct group by JavaAPI查询
import java.net.UnknownHostException; import com.mongodb.BasicDBList; import com.mongodb.BasicDBObje ...
- STL_算法_Heap算法(堆排)(精)
C++ Primer 学习中. . . 简单记录下我的学习过程 (代码为主) /***************************************** STL-算法--Heap算法 堆排序 ...
- Android 主界面长按创建快捷方式
Android中创建快捷方式主要有两种方式.一是在代码中直接加入生成桌面快捷方式的代码:二是通过小部件加入; 这篇文章主要讲另外一种方法! 1.通过在AndroidManifest文件里为Activi ...
- HDU 5303 Delicious Apples (2015多校第二场 贪心 + 枚举)
Delicious Apples Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- shell学习-while
1.shell while语句语法 while condition do statements done 2.shell while语句实例 #! /bin/sh 2 var1=1 3 while(( ...