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 ...
随机推荐
- java爬虫的selenium基础使用
实用博客 selenium java教程 具体项目运用 项目背景:从西安市人民政府网站上获取到县区新闻,从下图可以看出“区县热点”是需要在页面中进行点击的,这里页面使用的是javascript的函数 ...
- apache源码编译安装
源码安装apche 下载apache的源码包文件 访问http://mirror.bit.edu.cn/apache/httpd/,复制如下gz文件的链接地址,并使用wget下载到本地 wget -P ...
- ASP.NET-Razor语法03
ASP.NET MVC中使用Razor语法 @{} @{ string s ="super xiao lizi"; @s; // 里面的这个@代表着在页面上输出这个s // 我记 ...
- Android源代码解析之(十三)-->apk安装流程
转载请标明出处:一片枫叶的专栏 上一篇文章中给大家分析了一下android系统启动之后调用PackageManagerService服务并解析系统特定文件夹.解析apk文件并安装的过程,这个安装过程实 ...
- hadoop-05-mysql修改密码
hadoop-05-mysql修改密码 su root 1,service mysqld start 2,vi /var/log/mysqld.log #在这里面查找密码 3, mysql -uroo ...
- 【LeetCode OJ 34】Search for a Range
题目链接:https://leetcode.com/problems/search-for-a-range/ 题目:Given a sorted array of integers, find the ...
- Ubuntu下用glade和GTK+开发C语言界面程序(一)
前言:对于大学中计算机系的每年暑假的课设有太多想说的,能从中学到非常多东西,当然不排除打酱油的,这些能够掠过哦,凡事都打酱油.人生也是打酱油的吧. 2333. 对于大三曾经的课设一般的要求都是用C写的 ...
- HDU 4930 Fighting the Landlords(暴力枚举+模拟)
HDU 4930 Fighting the Landlords 题目链接 题意:就是题中那几种牌型.假设先手能一步走完.或者一步让后手无法管上,就赢 思路:先枚举出两个人全部可能的牌型的最大值.然后再 ...
- powershell无法拖动文件到命令行
PS C:\Program Files\PowerShell\6.0.0-beta.6> New-Service -Name LISA_43_Dev_Batch -DisplayName LIS ...
- Hints
If you played with the Fibonacci function, you might have noticed that the bigger the argument you p ...